MrC*_*MrC 14 java error-handling exception spring-boot
我做了一个小的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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器抛出异常:
@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;
}
Run Code Online (Sandbox Code Playgroud)
我的Spring Boot应用程序设置:
@SpringBootApplication
@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class)
@ComponentScan
public class QaApiApplication {
public static void main(String[] args) {
SpringApplication.run(QaApiApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
ServletInitializer:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(QaApiApplication.class);
}
}
Run Code Online (Sandbox Code Playgroud)
的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>
Run Code Online (Sandbox Code Playgroud)
如果我执行一个生成异常的调用,我会得到正确的响应,即带有预期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);
}
}
Run Code Online (Sandbox Code Playgroud)
这个对我有用。