Camel: how to check response http response Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Camel: how to check response http response without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am pretty new with Camel. I have been trying to submit a data (Json from a file) to a webservice. This is my code:

    public static void main(String args[]) throws Exception {
        // create CamelContext
        CamelContext context = new DefaultCamelContext();

        // add our route to the CamelContext
        context.addRoutes(new RouteBuilder() {
            public void configure() {

        // start the route and let it do its work

        // stop the CamelContext

Then the webservice will response with Json which can be {result:OK} or {result:FAIL}

Now, if a response has responseCode as 200, Camel will consider as success.

My question is, how can I have a validating process for responsed JSon so that if it is FAIL, Camel should not consider as success?

Solution Credit @Namphibian:

By adding processor and the end. This code has been tested:

        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                Message in = exchange.getIn();
                String msg = in.getBody(String.class);
                System.out.println("Response: " + msg);
                    // go to party
                    throw new Exception("test exception");


There are two broad strategies you can use to achieve this.

Processor Based:

Add a processor to the end of the route. In this processor do the check if the webservice then responds with a true or false value.

A processor would look something like this:

    package com.example;

    import java.util.Map;

    import org.apache.camel.Body;
    import org.apache.camel.Exchange;
    import org.apache.camel.Handler;
    import org.apache.camel.Headers;
    import org.apache.camel.Message;

    public class GeneralProcessor {

        public void PrepapreErrorImportReport

                @Headers Map hdr
                , Exchange exch

            //todo: Get the message as a string;

            Message in = exch.getIn();
            String msg = (String)in.getBody();
            // Now check if body contains failed or ok.
                //todo: go party the message was OK
               //todo: Oh Oh! Houston we have a problem



You can then modify your route to use this processor.

The Simple Expression Language

This is one way the other way is to use the simple expression language. See the example below on how to use this.

                .when(simple("${body} contains 'OK'")).to("activemq:okqueue")

Notice the simple("${body} contains 'OK'") piece of code. That is the power of simple.

Both approaches have uses.

We are here to answer your question about Camel: how to check response http response - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji