Apache Camel JSON Marshalling for POJO Java Bean

I think I have a simple question, but it seems he cannot understand.

I am calling POJO with a class created from unmarshalling JSON as a parameter to a method. The question is, how can I return a return from a method back to JSON?

My route is lower;

from("direct:start")
 .choice()
  .when(header("methodname").isEqualTo("listCases"))
   .unmarshal().json(JsonLibrary.Jackson, UserDetails.class)
   .to("bean:com.xxx.BeanA")
  .when(header("methodName").isEqualTo("listPersons"))
   .unmarshal().json(JsonLibrary.Jackson, CaseDetails.class)
   .to("bean:com.xxx.BeanB");

... and I call the route below:

ProducerTemplate template = camelContext.createProducerTemplate();
template.setDefaultEndpoint(camelContext.getEndpoint("direct:start"));
InvocationResult result = (InvocationResult)template.requestBodyAndHeader(payload, "methodName", methodName);

The payload is JSON, and methodName is either listCases or listPersons in this example.

The class My InvocationResult is general and contains the String attribute returnCode, as well as a reference to the object that I would like to convert to JSON. This object will differ depending on whether listCases or listPersons is running.

Thank,

Bic

+4
3

, ( ), choice(). choice() end() ( , ), , out .

, , :

public class JacksonTestRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("jetty:http://localhost:8181/foo").to("direct:foo");

        from("direct:foo")
        .unmarshal().json(JsonLibrary.Jackson, Foo.class)
        .choice()
            .when().simple("${body.foo} == 'toto'")
                .log("sending to beanA")
                .to("bean:beanA")
            .otherwise()
                .log("sending to beanB")
                .to("bean:beanB")
        // close the choice() block :
        .end()
        // per the javadoc for marshall(), "the output will be added to the out body" :
        .marshal().json(JsonLibrary.Jackson);
    }
}

public class Foo {
    private String foo; // Constructor and accessor omitted for brevity
}

public class Bar1 {
    private String bar1; // Constructor and accessor omitted for brevity
}

public class Bar2 {
    private String bar2; // Constructor and accessor omitted for brevity
}

public class BeanA {
    public Bar1 doSomething(final Foo arg) {
        return new Bar1(arg.getFoo() + "A");
    }
}

public class BeanB {
    public Bar2 doSomething(final Foo arg) {
        return new Bar2(arg.getFoo() + "B");
    }
}

POSTing {"foo":"toto"} {"bar1":"totoA"} ( logs sending to beanA).

POSTing {"foo":"titi"} {"bar2":"titiB"} ( logs sending to beanB).

+3

-

      <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jackson</artifactId>
            <version>2.14.1</version>
        </dependency>

JSON RouteBuilder -

JacksonDataFormat jsonDataFormat = new JacksonDataFormat(Employee.class);

routebuilder :

from("file:C:/inputFolder").doTry().unmarshal(xmlDataFormat).
        process(new MyProcessor()).marshal(jsonDataFormat).
        to("jms:queue:javainuse")

- Apache Camel - Marshalling/Unmarshalling XML/JSON Data

+2

, .marshal().json(JsonLibrary.Jackson) ( , )

0

Source: https://habr.com/ru/post/1661664/


All Articles