Reputation: 369
I am using Camel version 3.2.0 and Spring Boot Version 2.2.6.RELEASE .
I am using camel-http component to consume a rest service which return list of product in JSON format.
I am trying to store in csv file. Because of http response is not set in the out in exchange, the body is empty.
I am using the below route.
<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
}
It is printing the body first time, but not subsequent time through processor or log component.
I am not sure why response is not set in the body. Please help on this.
Upvotes: 0
Views: 1983
Reputation: 6853
The response type is a stream. When you log the response first time you consume that stream and the data is gone. You can either enable stream caching or convert the stream to a string (<convertBodyTo type="java.lang.String"/>
) before doing anything else with the message body.
Upvotes: 2
Reputation: 31
Use exchange.getOut().getBody(String.class) to get response body.
Please refer to the link: https://camel.apache.org/components/latest/http-component.html#_message_body
Upvotes: 0