简体   繁体   中英

Camel 2.14.1 Rest DSL mix JSON and XML response

I am using the new REST DSL in Apache Camel and have the following configuration:

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

I would like the api/handshake route to format the reply as XML and the api/message route to format the reply as JSON. With the configuration above the api/handshake responds correctly with XML but when I try to access api/message I get the following error 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

The getAllMessages method returns a collection of a simple POJO without any JAXB annotations. If I set bindingMode to json, the api/message works fine but of course api/handshake does not.

Is it possible to mix data formats like this? What do I need to change to get it working?

I got it working, and I solved it like this:

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

The trick was to add the bindingmode on the handshake route. I set json globally because the majority of my services will use JSon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM