首页 文章

Camel unmarshal Rest响应异常

提问于
浏览
2

我正面临着来自Rest调用的解组响应的问题

我的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

    删除日志记录后解组开始工作的原因是因为正文的类型为 InputStream ,这意味着在第一次访问它时将刷新流,在本例中将是日志 .

    如果,如您所说,您需要进行日志记录,然后在记录之前将强制转换添加到String,即紧跟在 to 之后 .

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

    Edit

    我还找到了this FAQ来解释这种现象 .

相关问题