Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-boot: RestControllerAdvise not reached

I've built a RestControlerAdvise:

@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ExceptionControllerAdvice {

    @ExceptionHandler({DocumentAlreadyExistsException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public cat.gencat.ctti.canigo.arch.web.rs.model.Error handleException(DocumentAlreadyExistsException e) {

        cat.gencat.ctti.canigo.arch.web.rs.model.Error error = new cat.gencat.ctti.canigo.arch.web.rs.model.Error();
        error.setCode(HttpStatus.BAD_REQUEST.value());
        error.setMessage(e.getMessage());
        return error;

    }

}

Nevertheless, it's never reached even though I raise an DocumentAlreadyExistException.

It's detected on boot:

2018-08-20 17:08:25.791 INFO 4941 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in exceptionControllerAdvice

Any ideas?

like image 498
Jordi Avatar asked Oct 30 '25 22:10

Jordi


1 Answers

I ran the same code in my application and its working fine, also please note

@ExceptionHandler methods on the Controller are always selected before those on any @ControllerAdvice instance. It is undefined what order controller-advices are processed.

@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomExceptionHandler  {

    @ExceptionHandler(value={Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public final ErrorMessage exceptionHandler(Exception e)
    {
        ErrorMessage msg=new ErrorMessage();
        msg.setError("its an error");

        return msg;
    }

also please refer the following link for more information : https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

like image 104
aditya gupta Avatar answered Nov 01 '25 11:11

aditya gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!