'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?
Solution 1:[1]
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
Solution 2:[2]
You may be handling the DocumentAlreadyExistException instead of throwing it out for spring to handle it.
Solution 3:[3]
Make sure that the Exception handler class is in the same package or inside the package where the controller exists.
For example: If the package where the controller is found is:
package com.test.component.controller;
Then use the same package for the handler or use this:
package com.test.component.controller.exceptionhandler;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | aditya gupta |
| Solution 2 | Jayesh |
| Solution 3 | Wael |
