我想在使用 Camel Rest 端点时记录原始的“原始”请求正文(例如 JSON)。执行此操作的正确方法是什么?
我的设置 (RouteBuilder) 如下所示:
restConfiguration().component("jetty")
.host(this.host)
.port(this.port)
.contextPath(this.contextPath)
.bindingMode(RestBindingMode.json);
rest("myService/").post()
.produces("application/json; charset=UTF-8")
.type(MyServiceRequest.class)
.outType(MyServiceResponse.class)
.to(SERVICE_CONTEXT_IN);
from(SERVICE_CONTEXT_IN).process(this.serviceProcessor);
我的问题是,就使用这种方法而言,将请求存储为 Exchange 属性等机制“太晚了”,任何处理器在路由中都太晚了,即绑定(bind)已经发生并消耗了要求。此外,CamelHttpServletRequest 的 InputStream 已被读取且不包含任何数据。
首先使用日志EIP的地方就在单处理器之前:
from(SERVICE_CONTEXT_IN).log(LoggingLevel.INFO, "Request: ${in.body}")
.process(this.serviceProcessor);
但此时 ${in.body}
已经是 MyServiceRequest
的一个实例。上面添加的日志简单地产生 Request: x.y.z.MyServiceRequest@12345678
。我想记录的是绑定(bind)到 POJO 之前的原始 JSON。
在 RestConfigurationDefinition 或 RestDefinition 中似乎没有启用记录“原始”请求的内置方法。
我可以摆脱自动 JSON 绑定(bind)并在专用处理器中手动读取 HTTP Post 请求的 InputStream、记录和执行手动解码等,但我想保留内置绑定(bind)。
最佳答案
我同意没有办法使用 Camel Rest 端点记录原始请求(我假设您的意思是有效负载在任何自动绑定(bind)之前通过线路)。
但取Roman Vottner考虑到这一点,您可以按如下方式更改您的 restConfiguration()
:
restConfiguration().component("jetty")
.host(this.host)
.port(this.port)
.componentProperty("handlers", "#yourLoggingHandler")
.contextPath(this.contextPath)
.bindingMode(RestBindingMode.json);
您的 #yourLoggingHandler
需要在注册表中注册并实现 org.eclipse.jetty.server.Handler
。请查看 Jetty 文档中的编写自定义处理程序 http://www.eclipse.org/jetty/documentation/current/jetty-handlers.html#writing-custom-handlers .
关于java - 使用 Camel Rest 时如何记录请求负载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40615073/