Camel 2.14.1 Rest DSL 混合 JSON 和 XML 响应

Camel 2.14.1 Rest DSL mix JSON and XML response

我在 Apache Camel 中使用新的 REST DSL 并具有以下配置:

restConfiguration().component("jetty").host("0.0.0.0").port(24999).bindingMode(RestBindingMode.auto).dataFormatProperty("prettyPrint", "true");

    rest("api/handshake")
            .post().id("Handshake Route").type(SessionRequest.class).outType(SessionResponse.class).to("bean:restHandshakeApi?method=testHandshake");

    rest("api/message").
            produces("application/json").
            get().id("GetMessages").to("bean:restMessageApi?method=getAllMessages");

我希望 api/handshake 路由将回复格式化为 XML,api/message 路由将回复格式化为 JSON。使用上面的配置 api/handshake 正确响应 XML 但是当我尝试访问 api/message 时,我收到以下错误消息:

java.io.IOException: org.apache.camel.InvalidPayloadException: No body available of type: javax.xml.bind.JAXBElement but has value: [] of type: java.util.ArrayList on: Message: []. Exchange[Message: []]
at org.apache.camel.converter.jaxb.JaxbDataFormat.marshal(JaxbDataFormat.java:141)
at org.apache.camel.processor.MarshalProcessor.process(MarshalProcessor.java:81)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)
at org.apache.camel.processor.MarshalProcessor.process(MarshalProcessor.java:50)
at org.apache.camel.processor.binding.RestBindingProcessor$RestBindingMarshalOnCompletion.onAfterRoute(RestBindingProcessor.java:338)
at org.apache.camel.util.UnitOfWorkHelper.afterRouteSynchronizations(UnitOfWorkHelper.java:154)
at org.apache.camel.impl.DefaultUnitOfWork.afterRoute(DefaultUnitOfWork.java:273)
at org.apache.camel.processor.CamelInternalProcessor$RouteLifecycleAdvice.after(CamelInternalProcessor.java:310)
at org.apache.camel.processor.CamelInternalProcessor$InternalCallback.done(CamelInternalProcessor.java:240)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:106)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:150)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.handler.ResourceHandler.handle(ResourceHandler.java:406)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:370)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:53)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.camel.InvalidPayloadException: No body available of type: javax.xml.bind.JAXBElement but has value: [] of type: java.util.ArrayList on: Message: []. Exchange[Message: []]
    at org.apache.camel.converter.jaxb.JaxbDataFormat.marshal(JaxbDataFormat.java:175)
    at org.apache.camel.converter.jaxb.JaxbDataFormat.marshal(JaxbDataFormat.java:138)
    ... 33 more

getAllMessages 方法returns 没有任何 JAXB 注释的简单 POJO 的集合。如果我将 bindingMode 设置为 json,api/message 可以正常工作,但当然 api/handshake 不会。

是否可以像这样混合数据格式?我需要更改什么才能使其正常工作?

我成功了,我是这样解决的:

restConfiguration().component("jetty").host("0.0.0.0").port(24999).bindingMode(RestBindingMode.json).dataFormatProperty("prettyPrint", "true");

rest("api/handshake").
bindingMode(RestBindingMode.xml).
        post().id("Handshake Route").type(SessionRequest.class).outType(SessionResponse.class).to("bean:restHandshakeApi?method=testHandshake");

rest("api/message").
        produces("application/json").
        get().id("GetMessages").to("bean:restMessageApi?method=getAllMessages");

诀窍是在握手路由上添加绑定模式。我全局设置了 json,因为我的大部分服务都将使用 JSon。