gpt4 book ai didi

java - Camel 解码休息响应异常

转载 作者:行者123 更新时间:2023-12-05 06:35:38 24 4
gpt4 key购买 nike

我正面临来自 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]

我试过向字符串添加强制转换:

  <convertBodyTo type="String"/>

但它导致了 BufferedStream 的异常。

日志显示响应正常:

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

如何解决解码问题?

最佳答案

删除日志后解码开始工作的原因是因为主体是 InputStream 类型,这意味着流将在第一次访问后被刷新,在这种情况下将是日志。

如果,如您所说,您需要进行日志记录,则将强制转换添加到 String before logging,即紧接在 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>

编辑

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

关于java - Camel 解码休息响应异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com