Spring Boot @ControllerAdvice An exception handler that does not return HTTP status text

I have GlobalExceptionHandlerone that catches exceptions and returns 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 help 404. But the HTTP response is as follows:

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

Missing part Not Found. This is the same for other errors. eg500 - Internal Server Error

Any ideas on how to enable this?

Update: Drop from Spring Download 1.4.0 to 1.3.7 fixed this

+4
source share
2 answers

From the release note :

Server header

HTTP- , server.server-header.

, .

+3
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler{

, spring.

@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) , ResponseEntity. , ResponseEntityExceptionHandler, .

+1

Source: https://habr.com/ru/post/1650047/


All Articles