Spring boot ErrorPageFilter将错误代码更改为HTTP 200 OK
似乎Spring Boot的ErrorPageFilter将我所有的HTTP状态代码都更改为200OK(除了4xx)。这仅在部署到Servlet容器时发生。这是一个错误还是我做错了什么 为了重现这一点,我创建了一个非常简单的Spring Boot应用程序:Spring boot ErrorPageFilter将错误代码更改为HTTP 200 OK,spring-boot,Spring Boot,似乎Spring Boot的ErrorPageFilter将我所有的HTTP状态代码都更改为200OK(除了4xx)。这仅在部署到Servlet容器时发生。这是一个错误还是我做错了什么 为了重现这一点,我创建了一个非常简单的Spring Boot应用程序: @SpringBootApplication @RestController public class ResponseErrorController extends SpringBootServletInitializer { pu
@SpringBootApplication
@RestController
public class ResponseErrorController extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ResponseErrorController.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ResponseErrorController.class);
}
@RequestMapping("/304")
public void lala(HttpServletResponse response) throws Exception {
response.sendError(304, "Not Modified");
}
}
当在浏览器中打开非嵌入式Tomcat(或Jetty)时,我总是得到200 OK(无内容)
经过一些调试后,我发现ErrorPageFilter没有将包装响应中的状态代码委托给真实响应。我知道我可以在我的控制器中返回ResponeEntity,但我的实际问题发生在另一个框架的Servlet上,该框架发送304-该控制器仅用于演示
其他人看到了吗?400以下的状态代码没有错误,应该通过
setStatus(int)
而不是sendError(int)
进行设置<因此,code>ErrorPageFilter仅处理状态代码>=400的sendError调用
下面是修复方法:
- 如果是您自己的代码:请使用
setStatus(int)
- 如果第三方servlet或筛选器通过
发送3xx状态代码:作为一种解决方法,您可以通过以下方式删除或替换senderError(int)
中的SpringBootServletilizer
:ErrorPageFilter
protected WebApplicationContext run(SpringApplication application) { application.getSources().remove(ErrorPageFilter.class); return super.run(application); }
response.setStatus()
。我们正在使用Tapestry,我看到他们用5.3解决了这个问题