0

Spring Boot by default returns a response body for exceptions that meets my needs out of the box:

{
    "timestamp": 1587794161453,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "javax.persistence.EntityNotFoundException",
    "message": "No resource exists for the given ID",
    "path": "/my-resource/1"
}

However, I would like to customize the response code for different types of exceptions thrown by my application. Some of the exceptions are not ones I own, so I can't just stick a @ResponseStatus annotation on the exception class. I've tries using an @ExceptionHandler method with @ResponseStatus, but that is overwriting the response body, which I don't wish to happen. For instance, I would like to map javax.persistence.EntityNotFoundException to return status code 404.

@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public void handleEntityNotFoundException(EntityNotFoundException e) {
    // This method returns an empty response body
}

This question is similar to mine, but it is also attempting to adjust the response body. I am hoping that there is a shorter, more idiomatic way of adjusting just the status code and not the body.

M. Justin
  • 14,487
  • 7
  • 91
  • 130

1 Answers1

1

It turns out that this answer to the question I mentioned had the exact solution needed to solve this problem, even though it didn't quite fully the question asked. The trick was dropping the use of @ResponseStatus from the method, and manually setting the status on the HttpServletResponse using HttpServletResponse.sendError(). This serves the standard Spring Boot exception response, but with the updated status code.

@ExceptionHandler
public void handleEntityNotFoundException(EntityNotFoundException e, HttpServletResponse response) throws IOException {
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
{
    "timestamp": 1587794161453,
    "status": 404,
    "error": "Not Found",
    "exception": "javax.persistence.EntityNotFoundException",
    "message": "No resource exists for the given ID",
    "path": "/my-resource/1"
}
M. Justin
  • 14,487
  • 7
  • 91
  • 130