【问题标题】:How to return Http Status-Line with Error Response?如何返回带有错误响应的 Http 状态行?
【发布时间】:2019-08-25 04:34:36
【问题描述】:

我正在开发一个 Spring-Boot 应用程序。对于每个响应,我想返回 http 状态行、标题和正文。根据标准,状态行看起来像:HTTP-Version SP Status-Code SP Reason-Phrase CRLF。 例如:Http/1.1 400 错误请求

我正在使用带有 VnDErrors 的 ResponseEntity,但状态行没有按照标准形成。我只能看到“Http/1.1 400”。这里缺少原因短语。

我已经尝试使用带有@ResponseStatus 注释的@ResponseBody,但没有运气达到预期的结果。

这是我正在使用的一段代码:

@ExceptionHandler(HttpRequestMethodNotSupportedException)
    ResponseEntity<VndErrors> httpRequestMethodNotSupportedException(ex) {
        LOGGER.error(ex.message)
        ResponseEntity.status(BAD_REQUEST).contentType(VND_ERROR).body(new 
        VndErrors(BAD_REQUEST, exceptionMessage))
    }

包含状态行的预期响应:“Http/1.1 400 Bad Request” 想知道这是否可以实现?如果是,那么我该如何继续这样做。

【问题讨论】:

    标签: spring rest spring-boot http httpresponse


    【解决方案1】:

    这是tomcat的标准行为见tomcat-8.5-changelog.html

    spring-boot-issue-6789

    RFC 7230 规定客户端应忽略 HTTP/1.1 响应消息中的原因短语。由于原因短语是可选的,因此 Tomcat 不再发送它。因此,系统属性 org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER 不再使用并已被删除。 (市场)

    【讨论】:

    • 好的。但是有没有办法发送它作为回应。可能是程序化的?
    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多