9

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

Chin Huang
  • 12,912
  • 4
  • 46
  • 47
Sikorski
  • 2,653
  • 3
  • 25
  • 46

2 Answers2

1

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) {

shaILU
  • 2,050
  • 3
  • 21
  • 40
0

Apache Camel provides a component to marshal and unmarshal POJO to and from JSON.

In your case, it would be :

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

By the way, you may need to configure your json library to do it and I suggest you take look at the official configuration.

victor gallet
  • 1,819
  • 17
  • 25