Spring Boot – ResponseStatusException
reason not returned in response body

At some point I have decided that I really have to return HTTP codes for some illegal invocations of my API. I have decided to use simple solution.

public String getResourceThatThrowsResponseStatusException() {
  throw new ResponseStatusException(
    "Not found thrown by getResourceThatThrowsResponseStatusException");

however, this solution has one, small drawback. You have to remember about putting


inside application.properties. Otherwise, reason will not be passed back. You can read some thoughts about that here.

Anyways, before I have learned that I can use this approach, I have already built something different. Solution based on @ControllerAdvice. Here, I have a custom exception handler that extracts info from the exception and passes it back to client as JSON message.

public class GlobalExceptionHandler {
  public ResponseEntity<ErrorDTO> generateNotFoundException(NotFoundException ex) {
    ErrorDTO errorDTO = new ErrorDTO();
    errorDTO.setTime(new Date().toString());

    return new ResponseEntity<ErrorDTO>(errorDTO, ex.getStatus());


You can find full solution here: