Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In the UserErrorService bean we build our custom error message, and set the HTTP error code to 400. This is important, as that tells rest-dsl that this is a custom error message, and the message should not use the output pojo binding (eg would otherwise bind to CountryPojo).

Catching JsonParserException and returning a custom error message

From Camel 2.14.1 onwards you return a custom message as-is (see previous section). So we can leverage this with Camel error handler to catch JsonParserException, handle that exception and build our custom response message. For example to return a HTTP error code 400 with a hardcoded message, we can do as shown below:

Code Block
onException(JsonParseException.class)
    .handled(true)
    .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
    .setHeader(Exchange.HTTP_CONTENT_TYPE, constant("text/plain"))
    .setBody().constant("Invalid json data");

 

Integration a Camel component with Rest DSL

...