我正在使用Camel版本3.2.0和Spring Boot版本2.2.6.RELEASE。
我正在使用camel-http组件来消耗Rest服务,该服务以JSON格式返回产品列表。我正在尝试存储在csv文件中。由于未设置http响应,所以主体为空。
我正在使用以下路线。
<route id="getProducts">
<from uri="quartz://groupName/timerName?cron=0 0/2 * 1/1 * ? *" />
<setHeader name="CamelHttpMethod">
<constant>GET</constant>
</setHeader>
<to uri="http://localhost:8080/product"></to>
<log message=" before processor \n Body ${body} \n headers ${headers}"/>
<!-- <process ref="processor" /> -->
<log message=" before convert \n Body ${body} \n headers ${headers}"/>
<marshal>
<bindy type="Csv" classType="com.basf.vo.Product" />
</marshal>
<log message=" after convert \n Body ${body}"/>
<to uri="file:\balachandar\?fileName=abc.csv"/>
<log message="Products Received \n ============================= \n"/>
</route>
<log message=" before processor \n Body ${body} \n headers ${headers}"/> => It is printing Body {"productId":1,"name":"Pink Ralph Lauren Polo Shirt","category":"Dress"} in the console
<log message=" before convert \n Body ${body} \n headers ${headers}"/> => It is not printing the body. Body is empty.
I used custom processor to know the body.
public void process(Exchange exchange) throws Exception {
String msg = exchange.getIn().getBody(String.class);
String msg1 = exchange.getMessage(String.class);
System.out.println("Rest Response is:->" + msg); // Rest Response is:->
System.out.println("Rest Response is:->" + msg1); // Rest Response is:->null
}
它是第一次打印主体,但不是以后通过处理器或日志组件打印。我不确定为什么身体没有反应。请对此提供帮助。
使用exchange.getOut()。getBody(String.class)获取响应正文。
请参阅链接:https://camel.apache.org/components/latest/http-component.html#_message_body
响应类型是流。首次记录响应时,您将消耗该流,并且数据已消失。您可以启用stream caching或将流转换为字符串(<convertBodyTo type="java.lang.String"/>
),然后再对消息正文执行其他任何操作。