4

I'm writing a Spring Boot Application. And I'm implementing the exception handling right now.

I got the following problem.

My exception handlers look like this:

@ControllerAdvice
public class SpecialExceptionHandler {

@ExceptionHandler(MissingServletRequestParameterException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseObject missingServletErrorHandler(HttpServletRequest req, MissingServletRequestParameterException exception) {

 //do something
return responseObject;
}}

And I got a general exception handler which looks

@ControllerAdvice
public class GeneralExceptionHandler {    

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseObject defaultErrorHandler(HttpServletRequest req, Exception exception)  {
// do something
return responseObject;
}}

But I got a problem: my application always runs into the GeneralExceptionHandler instead of the special handler unless I change the name of the GeneralExceptionHandler class to a name which comes alphabetically after the special exception handler (e.g. change 'GeneralExceptionHandler' to 'zGeneralExceptionHandler').

How can I resolve this issue?

1 Answer 1

4

You could try adding @Order(N) annotations on your ControllerAdvices, to force their registration order (where N is a int defining the order)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.