Spring Boot @ControllerAdvice 异常处理程序不返回 HTTP 状态文本 [英] Spring Boot @ControllerAdvice Exception Handler not returning HTTP Status Text

查看:93
本文介绍了Spring Boot @ControllerAdvice 异常处理程序不返回 HTTP 状态文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 GlobalExceptionHandler 可以捕获异常并返回 HTTP 错误代码.

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());
    }

    ...

}

这可以正常工作并以 404 响应.但是 HTTP 响应看起来像这样:

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

但应该返回:

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

Not Found 部分丢失.对于其他错误也是如此.例如500 - 内部服务器错误

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?

更新:从 Spring Boot 1.4.0 降级到 1.3.7 修复了这个问题

Update: Downgrading from Spring Boot 1.4.0 to 1.3.7 fixed this

推荐答案

来自 发行说明:

除非设置了 server.server-header 属性,否则不再设置服务器 HTTP 响应标头.

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.

这篇关于Spring Boot @ControllerAdvice 异常处理程序不返回 HTTP 状态文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆