0

Camel endpoint with choice and jsonpath works correctly when tried as an independent endpoint but when I introduce rest endpoint in route configuration choice stops filtering for the content-based routing of messages send from postman. Is there something that I am missing here?

I have tried directly sending the json message to endpoint and it correctly routes. But when adding restConfiguration and try bringing up the route it stops routing or sending to correct methods.

Main class-

    static String jsonMsg = "{\n" + 
    "    \"Header\": {\n" + 
    "        \"MessageType\": \"Request1\",\n" + 
    "        \"MessageID\": \"12345\",\n" + 
    "    }\n" + 
    "}";

public static void main(String[] args) throws Exception {

    CamelContext context = new DefaultCamelContext();
    context.addRoutes(new HelloRoute());
    context.start();

    //ProducerTemplate producer = context.createProducerTemplate();
    //producer.sendBody("direct:helloRoute", jsonMsg); 
}

HelloRoute class-

    @Override
public void configure() throws Exception {
    restConfiguration()
        .component("jetty")
        .host("0.0.0.0")
        .port("9281")
        .scheme("http")
        .componentProperty("minThreads", "1")
        .componentProperty("maxThreads", "16");rest("/helloCamel/").consumes("application/json").produces("application/json").post().to("direct:helloRoute");

from("direct:helloRoute") 
    .choice()
        .when().jsonpath("$.Header[?(@.MessageType == 'Request1')]",true)
            .bean(HelloRoute.class, "Route1")
        .when().jsonpath("$.Header[?(@.MessageType == 'Request2')]",true)
            .bean(HelloRoute.class, "Route2")
        .otherwise()                     
            .bean(HelloRoute.class,"otherwiseRoute")
        .endChoice();
 }

Currently, with rest if above JSON is changed to "MessageType": "Request2" then it jumps to otherwise clause instead of going to the "Route2" method.

Vikrant
  • 63
  • 1
  • 1
  • 8
  • Try adding `.convertBodyTo(String.class)` before the `choice` so the message body is read into memory and are re-readable for the predicates. An alternative is to turn on stream caching. See this FAQ: https://camel.apache.org/manual/latest/faq/why-is-my-message-body-empty.html – Claus Ibsen Sep 05 '19 at 05:41
  • @ClausIbsen Thank you. As you said, I added `.convertBodyTo(String.class)` and it works as expected. When I tried turning on stream caching in main class -`CamelContext context = new DefaultCamelContext(); context.setStreamCaching(true); context.addRoutes(new HelloRoute()); context.start();`with it doesn't work. – Vikrant Sep 05 '19 at 12:09

0 Answers0