Reputation: 275
hi~ i am using camel http component. and i can't extract body message.
here is my code
.log(LoggingLevel.INFO, "ToUri ===> ${body}")
.toD("${body}")
.log(LoggingLevel.INFO, "Result ===> ${body}")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
long startTime = System.currentTimeMillis();
Message inboundMessage = exchange.getIn();
Object body = exchange.getIn().getBody();
String msg = inboundMessage.getBody(String.class);
System.out.println("body:"+body);
System.out.println("getInBody msg:"+msg);
System.out.println("getInBody body:"+body.toString());
=======================================================================
body : org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@28936ba4
getInBody msg:
getInBody bodybodybody:org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@28936ba4
the log is good works. like this
09:56:53.523 INFO route1 - ToUri ===> https://translation.googleapis.com/language/translate/v2?key=tesetKey&source=en&target=ja&q=hi
09:56:54.545 INFO route1 - Result ===> {
"data": {
"translations": [
{
"translatedText": "こんにちは"
}
]
}
}
i want to extract translatedText using camel.
how can i handle CachedOutputStream and what is this?
i search camel doc.and cant understand.please give me a hint to solve my problem.
thanks.
Upvotes: 4
Views: 7360
Reputation: 81
you can use convertBodyTo(Class<?> type) method for that, like this
.log(LoggingLevel.INFO, "Result ===> ${body}")
.convertBodyTo(String.class)
.process(new Processor() { ... }
Upvotes: 0
Reputation: 21
You already took the stream data (with .log
) before calling processor. Stream data can only fetched once apparently. Try remove the log step and you can get in the processor:
.log(LoggingLevel.INFO, "Result ===> ${body}")
.process(new Processor() {
after spend 2 days experimenting
Upvotes: 2
Reputation: 55525
See stream-caching for information about CachedOutputStream
: http://camel.apache.org/stream-caching.html
To get the message body as string from the processor, you just do
String body = exchange.getIn().getBody(String.class);
That will tell Camel that you want the message as a String
and it will automatic covert the message body from CachedOutputStream
to String
. Then you can grab that text you want via regular Java code.
Also note there is jsonpath you can use to work with json data and get information, however its syntax can take a little bit to learn: http://camel.apache.org/jsonpath
Upvotes: 5