I have a Spring 4 application with multiple ControllerAdvices annotated with @Order(someValue). In addition, I have discovered a ControllerAdvice in one of my external libraries also annotated with @Order(someValue).
My understanding is that when a Controller throws an exception, the order of the ControllerAdvices dictates the order in which the ControllerAdvices are searched for that particular exception. I am now thinking it is also possible that there are other ControllerAdvices with @Order annotations in my external libraries. However, it is not feasible for me to download all the libraries and search for all the ControllerAdvices and check their Order values.
How do I know what Order value to put on a particular ControllerAdvice if I want it to catch exceptions before other ControllerAdvices? Should I be using a different approach for my use case?
See code below.
I want ExceptionHandlerControllerTwo to capture exceptions after ExceptionHandlerControllerOne and before ExceptionHandlerControllerThree.
I tried different Order values for ExceptionHandlerControllerTwo. Numbers 1 through 90 appeared to catch exceptions the way I wanted it to. There could be other ControllerAdvices in my external libraries that I do not know about.
In my app:
@ControllerAdvice
@Order(0)
public class ExceptionHandlerControllerOne {
// multiple @ExceptionHandler methods
}
@ControllerAdvice
@Order(80)
public class ExceptionHandlerControllerTwo {
// multiple @ExceptionHandler methods
}
@ControllerAdvice
@Order(90)
public class ExceptionHandlerControllerThree {
// multiple @ExceptionHandler methods
}
In an external library:
@ControllerAdvice
@Order(100)
@Slf4j
public class CatchAllExceptionHandlerController {
// multiple @ExceptionHandler methods
}