gpt4 book ai didi

java - 尝试处理 Spring Boot 应用程序中的异常时日志中出现 ErrorPageFilter 错误

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:26 25 4
gpt4 key购买 nike

我制作了一个小型 Spring Boot 应用程序,现在我正尝试添加我自己的异常处理。我遇到了一个问题,即使应用程序按预期工作,我也会在日志中收到错误消息。配置:

  • Tomcat 8(独立)
  • Spring Boot 1.2.3 版本
  • war 包装


异常处理程序如下所示:

@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);
}

}

我抛出异常的 Controller :

@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] o.s.boot.context.web.ErrorPageFilter: Cannot forward to error page for request [/environments/1] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false


启动/部署没有错误,据我所知,一切似乎都在工作。我怀疑有一些我没有正确覆盖的默认行为,但我还没有弄清楚到底是什么。

任何关于此的帮助/提示将不胜感激,因为我已经陷入了问题可能是什么。

最佳答案

如果您仍然使用 Spring-boot 版本 1.2.3(不是 1.4.2.RELEASE where solution provided),那么扩展 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);
}
}

它对我有用。

关于java - 尝试处理 Spring Boot 应用程序中的异常时日志中出现 ErrorPageFilter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29547233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com