Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Camel: Convert JSON to a POJO using Camel methods

I have a REST server which sends JSON in response body. I have recently started reading about Apache Camel. I use following to send requests to my REST service.

from("direct:start").setHeader("token", simple("234da"))
                            .to("http://localhost:8088/foo/bar/?foo1=bar1");

Now the response will be a JSON, is there any way I get this JSON directly into a POJO using some method ahead of to() (something like this)?

to("http://localhost:8088/foo/bar/?foo1=bar1").toPOJO();

I would prefer a non Spring solution.

Thanks

like image 205
Sikorski Avatar asked Apr 17 '12 12:04

Sikorski


1 Answers

Little details from my side - although late

Create jsonFormatter and then unmarshal with class you need
JsonDataFormat jsonDataFormat = new JsonDataFormat(JsonLibrary.Jackson);
this can be used in marshalling

from("direct:consume-rest")
.log("calling bean method...")
.to("http://localhost:8080/greeting?name=baba")
//.process(svProcessor) // any extra process if you want
.unmarshal().json(JsonLibrary.Jackson, Greeting.class)
.bean(GreetingHelper.class, "print")
.log("converted to bean ...")
.end()
;

Helper class method
public void print (@Body Greeting greeting) {

like image 175
shaILU Avatar answered Nov 14 '22 22:11

shaILU