Camel unmarshal Rest响应异常

时间:2018-04-04 14:25:44

标签: java apache-camel

我面临来自Rest call

的解组响应问题

我的camel-context.xml看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:camel="http://camel.apache.org/schema/spring"
    xmlns:cxf="http://camel.apache.org/schema/cxf"
    xmlns:osgi="http://www.springframework.org/schema/osgi"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf-2.8.3.xsd">

    <bean class="com.myapp.MyProcessor" id="myResponseProcessor"/>

    <camelContext id="camelId" xmlns="http://camel.apache.org/schema/spring">

        <camel:route id="myServiceCreate">
            <!-- SKIPPING PREPARATION PART -->
            <log message="BODY ----- ${body}"/>
            <marshal>
                <json library="Jackson"/>
            </marshal>
            <setHeader headerName="CamelHttpMethod">
                <constant>POST</constant>
            </setHeader>
            <to uri="{{services.myuri}}/create"/>
            <log message="Message: ${body}"></log>
            <unmarshal>
                <json library="Jackson"  unmarshalTypeName="com.myapp.MyPojo"/>
            </unmarshal>
            <process id="_processMyResponse" ref="myResponseProcessor"/>
        </camel:route>
    </camelContext>
</beans>

结果我得到了一个例外

Caused by: com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@68de2df1; line: 1, column: 0]  

我尝试过向String添加强制转换:

  <convertBodyTo type="String"/>

但它在BufferedStream中引起了异常。

日志显示响应正常:

17:13:25.532 [http-nio-0.0.0.0-8080-exec-1] INFO  myServiceCreate - Message: {"collectionId":"123"}

如何修复解组?

1 个答案:

答案 0 :(得分:0)

在删除日志记录后解组开始工作的原因是因为正文是InputStream类型的,这意味着在第一次访问它时将刷新流,在这种情况下将是日志。 / p>

正如您所说,如果需要记录,则在记录之前,即在to之后立即将强制转换添加到String

<to uri="{{services.myuri}}/create"/>
<convertBodyTo type="java.lang.String" />
<log message="Message: ${body}"></log>
<unmarshal>
    <json library="Jackson"  unmarshalTypeName="com.myapp.MyPojo"/>
</unmarshal>

修改

我还找到了解释这种现象的this常见问题解答。