5

I have a GlobalExceptionHandler that catches exceptions and returns a HTTP Error codes.

@ControllerAdvice
@Component
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    ...

    // 404 - Not Found
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public void requestHandlingNoHandlerFound(HttpServletRequest request, Exception exception) {
        logger.error("Error Not Found (404): " + exception.getMessage());
    }

    ...

}

This works correctly and responds with a 404. But the HTTP response looks like this:

HTTP/1.1 404 
X-Application-Context: application:8080
Content-Length: 0
Date: Wed, 03 Aug 2016 14:36:52 GMT

But should return:

HTTP/1.1 404 Not Found
X-Application-Context: application:8080
Content-Length: 0
Date: Wed, 03 Aug 2016 14:36:52 GMT

The Not Found part is missing. This is the same for other errors. e.g. 500 - Internal Server Error

Any ideas in how to include this?

Update: Downgrading from Spring Boot 1.4.0 to 1.3.7 fixed this

1
  • I really, really hope you mean Spring Boot 1.4.0 -> 1.3.7 Commented Aug 3, 2016 at 14:56

2 Answers 2

3

From the release notes:

Server header

The Server HTTP response header is no longer set unless the server.server-header property is set.

This seems like it might be the cause of your issue.

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

3 Comments

Thanks, what would this need to be set to?
Hah, my bad - applies to something completely different. This looks like a regression, and it would probably be worth reporting a bug for it.
From tomcat.apache.org/tomcat-8.5-doc/changelog.html: RFC 7230 states that clients should ignore reason phrases in HTTP/1.1 response messages. Since the reason phrase is optional, Tomcat no longer sends it.
1
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler{

by extending this class you can override default spring behavior.

@ExceptionHandler({NoHandlerFoundException.class,EntityNotFoundException.class})
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex,final WebRequest request) {
        final MyError myError= new MyError (HttpStatus.NOT_FOUND, ex);
        return handleExceptionInternal(ex, myError, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
}

@ResponseStatus(HttpStatus.NOT_FOUND) is ok, but to get more control over response use ResponseEntity. Also by extending ResponseEntityExceptionHandler you get access to bunch of preconfigured exception handlers.

1 Comment

I'll give this a go. Thanks :)

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.