Apache Camel: Convert JSON to a POJO using Camel methods

15,895

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

Share:
15,895
Sikorski
Author by

Sikorski

Play, Code, Eat ! SOreadytohelp

Updated on June 15, 2022

Comments

  • Sikorski
    Sikorski almost 2 years

    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