Camel 2.14.1 Rest DSL混合JSON和XML响应

时间:2015-01-04 23:22:52

标签: java xml json rest apache-camel

我在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$1.run(SelectChannelEndPoint.java:53)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.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方法返回一个简单POJO的集合,不带任何JAXB注释。如果我将bindingMode设置为json,则api / message工作正常,但当然api / handshake不会。

是否可以混合这样的数据格式?我需要做些什么才能让它发挥作用?

1 个答案:

答案 0 :(得分:0)

我得到了它的工作,我解决了这个问题:

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");

诀窍是在握手路线上添加bindingmode。我将json设置为全局,因为我的大部分服务都将使用JSon。