You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Stephen Bassett <sb...@gmail.com> on 2021/03/03 00:48:53 UTC

Camel rest route w Spring ControllerAdvice?

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<String> 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<String> 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");
    }
}

Re: Camel rest route w Spring ControllerAdvice?

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

They are 2 different things - one is spring specific. And the other is Camel.
You cannot magically have spring exception thingy know about camel
route exceptions.


On Wed, Mar 3, 2021 at 1:49 AM Stephen Bassett <sb...@gmail.com> wrote:
>
> 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<String> 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<String> 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");
>     }
> }



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2