Java Camel框架:丢失处理器中的消息体

时间:2014-07-13 00:06:39

标签: java json jackson apache-camel

所有

这是一条简单的路线:

 <route>
    <from uri="jetty://http://0.0.0.0:9090/myproject" />
    <setExchangePattern pattern="InOnly" />
    <process ref="JsonValidator"/> 
    <unmarshal>
       <json library="Jackson" unmarshalTypeName="com.myproject.JsonPojo"/>
    </unmarshal>
    ...
 </route>

JsonValidator是一个简单的Java bean,我扩展了处理器。在这里,我想确保在使用Jackson将JSON解组到我的POJO之前继续进行unmarshal调用之前传递所有必填字段。

我现在所做的一切只是一行:

  public void process(Exchange exchange) throws Exception {
      String input = exchange.getIn().getBody(String.class);
  }

简单地调用exchange.getIn()。getBody(String.class)导致我的路径中的下一个(解组)步骤抛出错误,说没有任何解组。事实上,我通过在JsonValidator之后添加另一个处理器来测试这个 - 在那里,交换体不是null但是它是空的。

这是错误:

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
         at [Source: org.apache.camel.converter.stream.InputStreamCache@78f0a00a; line: 1, column: 1]
            at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
            at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:2931)
            at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2873)
            at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2080)
            at org.apache.camel.component.jackson.JacksonDataFormat.unmarshal(JacksonDataFormat.java:105)
            at org.apache.camel.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:65)
            at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
            at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)
            at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
            at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)
            at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)
            at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
            at org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:151)
            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$CachedChain.doFilter(ServletHandler.java:1496)
            at org.eclipse.jetty.servlets.MultiPartFilter.doFilter(MultiPartFilter.java:136)
            at org.apache.camel.component.jetty.CamelFilterWrapper.doFilter(CamelFilterWrapper.java:44)
            at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1467)
            at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:499)
            at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
            at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
            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.Server.handle(Server.java:370)
            at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
            at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982)
            at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043)
            at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865)
            at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
            at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
            at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667)
            at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
            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:744)

我错过了什么?

1 个答案:

答案 0 :(得分:7)

Streams可以(通常)只读一次。阅读后,您必须将结果存储在交换中:

public void process(Exchange exchange) throws Exception {
    String input = exchange.getIn().getBody(String.class);
    exchange.getIn().setBody(input);
}

或者,您可以让Camel按照Camel documentation中的说明进行缓存。