Apache camel dynamic routing

709 views Asked by At

I have following Apache camel rest service(/sales) that internally calls another rest service(/getOrders) and get list of objects. Am able to print JSON response in the processor but getting java objects in response while trying from postman. Could anyone pls help me to resolve the issue. Attaching the response log for ref..

@Component
public class ApplicationResource extends RouteBuilder {

    @Autowired
    private OrderService service;

    @BeanInject
    private OrderProcessor processor;

    @Override
    public void configure() throws Exception {
        restConfiguration().component("servlet").port(9090).host("localhost");

        rest().get("/getOrders").produces(MediaType.APPLICATION_JSON_VALUE).route().setBody(() -> service.getOrders());


        rest().get("/sales").produces(MediaType.APPLICATION_JSON_VALUE).route()
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .toD("http://localhost:9090/getOrders?bridgeEndpoint=true").convertBodyTo(String.class).marshal()
                .json(JsonLibrary.Jackson, Order.class).to("log:foo?showHeaders=true");;
        ;

    }

}

enter image description here

3

There are 3 answers

0
rplg On BEST ANSWER
Solvedddd !!! i did two things as follows,May be use full for some one

1,bindingMode(RestBindingMode.auto) - RestBindingMode changes to auto from json
2, Added this in the main service(/getOrders).marshal().json(JsonLibrary.Jackson);

@Component
public class ApplicationResource extends RouteBuilder {

    @Autowired
    private OrderService service;

    @BeanInject
    private OrderProcessor processor;

    @Override
    public void configure() throws Exception {
        restConfiguration().component("servlet").port(9090).host("localhost").bindingMode(RestBindingMode.auto);

        rest().get("/getOrders").produces(MediaType.APPLICATION_JSON_VALUE).route().setBody(() -> service.getOrders())
                .marshal().json(JsonLibrary.Jackson);

        rest().get("/sales").produces(MediaType.APPLICATION_JSON_VALUE).route()
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .toD("http://localhost:9090/getOrders?bridgeEndpoint=true").convertBodyTo(String.class)
                .log("body = ${body}");
        ;
        ;

    }

}
0
Sneharghya Pathak On

This works for me.

First, I needed to set the bindingMode as RestBindingMode.json in the restConfiguration.

Secondly, instead of marshal(), you need to use unmarshal().

Third, since you are returning a list of orders, .json(JsonLibrary.Jackson, Order.class) will not be sufficient to unmarshal the list of orders. You need to use a custom format which will be able to unmarshal the list of orders into a json array. This you need to do using JacksonDataFormat format = new ListJacksonDataFormat(Order.class);

    @Override
    public void configure() {

        JacksonDataFormat format = new ListJacksonDataFormat(Order.class);
        restConfiguration().component("servlet").port(9090).host(localhost).bindingMode(RestBindingMode.json);

        rest()
            .get("/getOrders")
            .produces(MediaType.APPLICATION_JSON_VALUE)
            .route()
                .process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    exchange.getMessage().setBody(service.getOrders());
                }})
            .to("log:getOrders?showHeaders=true&showBody=true");

        rest()
            .get("/sales")
            .produces(MediaType.APPLICATION_JSON_VALUE)
            .route()
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .toD("http://localhost:9090/getOrders?bridgeEndpoint=true")
                .unmarshal(format)
                .to("log:sales?showHeaders=true&showBody=true");
    }
5
Guillaume WEILL On

You should remove the last .endRest() on "direct:bye" route. I think you get the rest response before calling your Processor.