如何返回带有错误响应的Http状态行?

问题描述 投票:0回答:1

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

我正在使用带有VnDErrors的ResponseEntity但是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”想知道这是否可以实现?如果是,那么我如何继续做同样的事情。

spring rest spring-boot http httpresponse
1个回答
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不再使用且已被删除。 (Markt的)

© www.soinside.com 2019 - 2024. All rights reserved.