Java Spring未在响应时设置statusText
我正在使用一些第三方的东西,要求在响应上设置statusText。无论我做什么,它都只是Java Spring未在响应时设置statusText,java,spring-mvc,spring-boot,Java,Spring Mvc,Spring Boot,我正在使用一些第三方的东西,要求在响应上设置statusText。无论我做什么,它都只是“。我使用的是Spring4.3.4和Spring-Boot1.4.2 我尝试将HttpServletResponse作为控制器方法中的参数,然后调用senderro如下: public void foo(@PathVariable String myVar, HttpServletResponse response) throws IOException { //... other stuff re
“
。我使用的是Spring4.3.4和Spring-Boot1.4.2
我尝试将HttpServletResponse
作为控制器方法中的参数,然后调用senderro
如下:
public void foo(@PathVariable String myVar, HttpServletResponse response) throws IOException
{
//... other stuff
response.sendError(200, "OKOKOKOKOK");
}
{"timestamp":1486068130749,"status":200,"error":"OK","message":"OKOKOKOKOK","path":"/my/path"}
但是,当实际响应返回到浏览器时,响应如下所示:
public void foo(@PathVariable String myVar, HttpServletResponse response) throws IOException
{
//... other stuff
response.sendError(200, "OKOKOKOKOK");
}
{"timestamp":1486068130749,"status":200,"error":"OK","message":"OKOKOKOKOK","path":"/my/path"}
并且状态文本仍然是
”
。我知道Spring必须在发送回客户端之前生成此响应。我怎样才能防止这种情况?有什么方法可以为statusText设置一个非空字符串吗?我想您一定是指Servlet容器发送的HTTP状态行。
从Tomcat 8.5开始,不再编写HTTP原因短语,因为它不是必需的,也不应该被HTTP客户端读取:请参阅。1.4.2版本的Spring boot使用嵌入的Tomcat 8.5.6,其中删除了响应文本。因此,您得到的statusText为“” 最初,原因短语被Tomcat-8.5删除 但是,后来提供了一个补丁,用Http连接器中的属性对其进行配置 您只需设置
sendReasonPhrase=“true”
属性,它就会像以前一样工作
使用以下任一方法进行配置: 方法1:更新…/apache-tomcat-8.5.x/conf/server.xml以包含以下属性:
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" sendReasonPhrase="true" />
注意:此配置将在Tomcat-9.0中删除(即:不会发送响应消息) 参考文献:
RFC 7230声明客户端应忽略HTTP/1.1响应消息中的原因短语。由于原因短语是可选的,Tomcat不再发送它代码>