[英]ErrorPageFilter error in log when trying to handle exception in a Spring Boot app
我做了一个小的Spring Boot应用程序,现在我正在尝试添加自己的异常处理。 即使应用程序按预期工作,我也遇到了日志中出现错误的问题。 配置:
异常处理程序如下所示:
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NotFoundException.class)
@ResponseBody ErrorInfo handleNotFoundRequest(HttpServletRequest req, Exception ex) {
return new ErrorInfo(req.getRequestURL().toString(), ex);
}
}
我的控制器抛出异常:
@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = {"application/json"})
public @ResponseBody
HashMap environment(@PathVariable("id") long id) {
HashMap<String, Object> map = new HashMap();
Environment env = environmentService.getEnvironment(id);
if(env == null) throw new NotFoundException("Environment not found: " + id);
map.put("environment", env);
return map;
}
我的Spring Boot应用程序设置:
@SpringBootApplication
@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class)
@ComponentScan
public class QaApiApplication {
public static void main(String[] args) {
SpringApplication.run(QaApiApplication.class, args);
}
}
ServletInitializer:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(QaApiApplication.class);
}
}
的pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
......
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如果我执行一个生成异常的调用,我会得到正确的响应,即带有预期JSON的404。 但是我在日志中看到ErrorPageFilter错误:
2015-04-09 21:05:38.647 ERROR 85940 --- [io-8080-exec-16] osboot.context.web.ErrorPageFilter:无法转发到请求[/ environments / 1]的错误页面,因为响应已经一直致力于。 因此,响应可能具有错误的状态代码。 如果您的应用程序在WebSphere Application Server上运行,则可以通过将com.ibm.ws.webcontainer.invokeFlushAfterService设置为false来解决此问题
启动/部署没有错误,就我所见,一切似乎都有效。 我怀疑有一些默认行为,我没有正确地覆盖,但我还没弄清楚到底是什么。
任何有关这方面的帮助/提示将非常感激,因为我已经陷入了问题所在。
如果您仍然使用Spring-boot版本1.2.3(而不是提供解决方案的 1.4.2.RELEASE),那么扩展SpringBootServletInitializer
并覆盖方法run
如下:
@SpringBootApplication
@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class)
@ComponentScan
public class QaApiApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(QaApiApplication.class, args);
}
@Override
protected WebApplicationContext run(SpringApplication application) {
application.getSources().remove(ErrorPageFilter.class);
return super.run(application);
}
}
这个对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.