简体   繁体   English

Spring Boot @ControllerAdvice异常处理程序不返回HTTP状态文本

[英]Spring Boot @ControllerAdvice Exception Handler not returning HTTP Status Text

I have a GlobalExceptionHandler that catches exceptions and returns a HTTP Error codes. 我有一个GlobalExceptionHandler捕获异常并返回HTTP错误代码。

@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 . 这可以正常工作,并以404响应。 But the HTTP response looks like this: 但HTTP响应如下所示:

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. 缺少Not Found部分。 This is the same for other errors. 其他错误也是如此。 eg 500 - Internal Server Error 例如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 更新:从Spring Boot 1.4.0降级到1.3.7修复此问题

From the release notes : 发行说明

Server header 服务器头

The Server HTTP response header is no longer set unless the server.server-header property is set. 除非设置了server.server-header属性,否则不再设置Server HTTP响应头。

This seems like it might be the cause of your issue. 这似乎可能是您的问题的原因。

@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 . @ResponseStatus(HttpStatus.NOT_FOUND) ,但为了更好地控制响应,请使用ResponseEntity Also by extending ResponseEntityExceptionHandler you get access to bunch of preconfigured exception handlers. 此外,通过扩展ResponseEntityExceptionHandler您可以访问一堆预先配置的异常处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot @ControllerAdvice异常处理程序没有触发 - Spring Boot @ControllerAdvice exception handler not firing Spring @ControllerAdvice异常处理程序返回404错误而不是视图 - Spring @ControllerAdvice exception handler returning 404 error instead of view ControllerAdvice 的异常处理程序在使用 Spring Boot 的 Rest API 获取请求中不起作用。 如何解决? - Exception Handler of ControllerAdvice Not working in Rest API Get Request using Spring Boot. How to resolve? ControllerAdvice 无法在 Spring 引导中捕获自定义异常 - ControllerAdvice cannot catch custom exception in Spring Boot Spring 使用@ControllerAdvice 和@CircuitBreaker 启动微服务异常行为 - Spring boot microservices exception behaviour with @ControllerAdvice & @CircuitBreaker Spring引导不使用@ControllerAdvice覆盖Exception - Spring boot not overriding Exception using @ControllerAdvice Spring 引导:无法在 @ControllerAdvice 中捕获异常 - Spring Boot: Cannot catch exception in @ControllerAdvice 如何防止 Spring Rest API 异常处理程序返回 HTTP 状态 400 的堆栈跟踪 - How to Prevent Stack Trace from Spring Rest API Exception Handler returning HTTP status 400 Spring Boot 从异常处理程序返回 401 状态的自定义对象 - Spring Boot Returning 401 Statused Custom Object From Exception Handler Spring Boot 1.4.3和@ControllerAdvice - Spring Boot 1.4.3 and @ControllerAdvice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM