I have a Spring app. I created an ExceptionHandler class and annotated it
with @ControllerAdvice. Exceptions thrown from my app are handled there.
However, an exception thrown from within a Camel route that is a
@RestController and has a restI() route will not be handled in my
ExceptionHandler class.
Has anyone else done this? It would be nice if I could put all of my
handling in one place in the ControllerAdvice. I am not sure why the Camel
rest route is not handled.
Thanks,
Steve Bassett
@RestControllerAdvice
public class ExceptionTranslator {
@ExceptionHandler(Exception.class)
public ResponseEntity handleThrowable(Exception exception) {
final String description = "general exception handler";
HttpStatus statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
final String jsonBody = getJsonResponse(description, statusCode,
exception);
return new ResponseEntity<>(jsonBody, statusCode);
}
@ExceptionHandler(MyInternalException.class)
public ResponseEntity handleMyInternalException(MyInternalException
exception) {
...
more exception handlers
}
@RestController
public class MyRestRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
rest()
.get("/hello/route")
.produces("text/plain")
.to("direct:hello-sub-route");
from("direct:hello-sub-route")
.process(exchange -> {
throw new IOException(" IOException -");
})
.transform().simple(" hello sub-route -")
.to("log:my-log");
}
}