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.

2
  • 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: camel.apache.org/manual/latest/faq/… Commented Sep 5, 2019 at 5: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. Commented Sep 5, 2019 at 12:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.