如何 return Http Status-Line 错误响应?

How to return Http Status-Line with Error Response?

我正在开发 Spring-Boot 应用程序。对于每个响应,我都希望 return http Status-Line、Headers 和 Body。根据标准,Status-Line 看起来像:HTTP-Version SP Status-Code SP Reason-Phrase CRLF。 例如:Http/1.1 400 错误请求

我将 ResponseEntity 与 VnDErrors 一起使用,但 Status-Line 未按照标准形成。我只能看到 "Http/1.1 400"。这里缺少 Reason-Phrase。

我已经尝试使用带有@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))
    }

预期的响应Status-Line:"Http/1.1 400 Bad Request" 想知道这是可以实现的吗?如果是,那么我该如何继续做同样的事情。

这是 tomcat 的标准行为,请参阅 tomcat-8.5-changelog.html

spring-boot-issue-6789

RFC 7230 states that clients should ignore reason phrases in HTTP/1.1 response messages. Since the reason phrase is optional, Tomcat no longer sends it. As a result the system property org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER is no longer used and has been removed. (markt)