4

After reading some blog posts about making a custom exception handler for Spring, I wrote the following class:

@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseEntity<Object> exceptionHandler(Exception e) {
        HashMap<String, Object> msg = new HashMap<>(2);
        msg.put("error", HttpStatus.PRECONDITION_FAILED.value());
        msg.put("message", "Something went wrong");
        return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
    }
}

The intent is to send msg in the JSON response instead of giving away the Spring exception what was thrown for whatever reason.

This class isn't working, however.

When I hit, say, and invalid endpoint for my server API, I get the default response payload:

{
  "timestamp": 1449238700342,
  "status": 405,
  "error": "Method Not Allowed",
  "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
  "message": "Request method 'POST' not supported",
  "path": "/bad_enpoint"
}

What am I missing?

Thanks.

2
  • Can you elaborate on "isnt't working"? It doesn't get called? It does, but it fails? Commented Nov 15, 2016 at 21:03
  • I updated the question with more info. It doesn't get called, I still the Spring default JSON back with the Spring expection being "exposed" Commented Nov 15, 2016 at 21:04

2 Answers 2

3

Your handler will not be called because you want to map Exception to your custom error response but Spring MVC most likely already has one exception handler registered for Exception class. It also has one that handles HttpRequestMethodNotSupportedException for sure.

It is not a great idea however, to overwrite entire Spring MVC exception handling/mapping anyway. You should only care about specific exceptions - ones that you define.

Please read this article for a bit more insight into Spring MVC exception handling.

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

3 Comments

Thanks for the article, I'm fine with Spring having its own handlers. I don't like, however, the fact that Spring gives away too much about the internals of an application by sending a response with stuff like "exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
Ok it is a valid point. Try to add @Order annotation with the HIGHEST PRECEDENCE set. See this answer: stackoverflow.com/questions/19498378/…
This link to the use of @Order is exactly what I needed.
2
  1. You don't need to extend ResponseEntityExceptionHandler to make it work.
  2. Setting two HttpStatuses is reaaaaaly bad idea.
    @ControllerAdvice
    public class RestExceptionHandler  {
        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public ResponseEntity<String> exceptionHandler(Exception e) {
            return new ResponseEntity<>("Something went wrong", HttpStatus.BAD_REQUEST);
        }
    }

5 Comments

I still get the same JSON response (see bottom of my question) instead of "Something went wrong"
The @ControllerAdvice is aspect for controllers. Your AJAX call didn't even get to controller - from response I guess you don't have POST implemented on /bad_enpoint controller (or no controller at all)
I have a @RestController class that defines the endpoints. Should @RestController and @ControllerAdvice be annotated on the same class?
No, consider @ControllerAdvice class as aspect for all controllers. Could you paste your @RestController class?
Unfortunately I can't, it is a proprietary application. Thank you for your help.

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.