Question

I am working on an Apache Camel project. Basically the Jetty endpoint takes a Http Post request and the message goes through a few steps of transformation in the route. The last step of transformation is through JAXB, which converts Java object into XML. The Java DSL is below

final DataFormat jaxb = new JaxbDataFormat("sample");
from("jetty:http://localhost:8888/foo")
.unmarshal(format).split(body()).marshal(jaxb)

My problem is that when I send a POST request to the localhost URL, the HTTP response is string [sample.Claims@b68e0e], not the XML I expected. This is the JAXB object ID. When I changed the DSL to

from("jetty:http://localhost:8888/foo")
.unmarshal(format).split(body()).marshal(jaxb).to("stream:out")

I can see the XML print out correctly in the stdout. I don't know how to make the HTTP Response to contain XML instead of the Object ID. Any help is appreciated.

Update:

I want to clarify what I try to accomplish. I need to convert a delimited string to an xml document. The post message to Jetty endpoint is a delimited string. The route first uses BeanIO to convert the string into a POJO and the POJO into XML using JAXB. Even though the post message is a single line string, I have to use split() because BeanIO by default dealing with multi-line flat file. I followed @Peter's suggestion by adding aggegate after the jaxb marshaling as below

from("jetty:http://localhost:8888/transformProxy/ECSProxy")
.unmarshal(format).split(body()).marshal(jaxb)
.aggregate(constant("1"),new MyAggregationStrategy())
.completionSize(1).to("stream:out");

but it does not seem to make any difference. I still get [sample.Claims@1a631c2] as the Http response body, while the stdout prints out the correct xml document. I am not sure how/when the response body of the jetty endpoint is set.

Was it helpful?

Solution

I made it work by moving the aggregate strategy as the second parameters of the split. For details, please see https://camel.apache.org/splitter.html. Search for the "Split aggregate request/reply sample"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top