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
The most basic way of returning an error message from a REST API is to use the @ResponseStatus annotation. We can add the error message in the annotation's reason field. Although we can only return a generic error message, we can specify exception-specific error messages.
The @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With