Apache camel "onException" not returning JSON response

214 Views Asked by At

I am using apache camel to expose rest endpoint from my application. When rest endpoint is successfully executed it is returning expected JSON response like below.

{
  "key1": "value1",
  "key2": 123
}

when there is an exception, onException camel clause get executed and in that case its not returning json response, instead it return java object representation like below

com.mypackage.Response@27f674d

Sample route definition as below.

    rest("")
    .consumes("application/json")
    .produces("application/json")    
    .post("/endpoint")
    .bindingMode(RestBindingMode.json)
    .type(Request.class)
    .outType(Response.class)
    .to("direct:endpoint")

Route endpoint defination

onException(Exception.class)
.process(new ExceptionProcessor())
.convertBodyTo(Response.class)
.handled(true);

from("direct:endpoint")
.streamCaching()
.process(...)
.process(...)
.end()

ExceptionProcessor sample implementation.

public class ExceptionProcessor implements Processor{
    public void process(Exchange exchange) throws Exception{
        Response res = new Response();
        res.setKey1("value1");
        res.setKey2(123);
        exchange.getMessage().setBody(res);
    }
}

Why onException is returning response as java object string representation? How can it return JSON response in case of exception?

0

There are 0 best solutions below