@ControllerAdvice不处理异常

时间:2015-09-12 19:15:48

标签: java spring rest exception-handling spring-boot

在我在Spring Boot中创建的Rest API中,我试图指出尚未找到资源抛出异常并使用@ControllerAdvice进行异常处理:

@ControllerAdvice
class GlobalControllerExceptionHandler {

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NotFoundException.class)
    public void notFound() {
        // do something...
    }

}

我的异常类:

@ResponseStatus(HttpStatus.NOT_FOUND)
public final class NotFoundException extends RuntimeException {

    public NotFoundException() {
    }

    public NotFoundException(String message) {
        super(message);
    }
}

和测试方法:

@RequestMapping(value = "/no", method = RequestMethod.GET)
    public void notExists() {
        throw new NotFoundException();
    }

但是HTTP 404投掷HTTP 500并且GlobalControllerExceptionHandler未激活。

更新1:

来自catalina.out

  

2015-09-12 22:42:59.510 ERROR 71872 --- [o-8080-exec-140]   o.s.boot.context.web.ErrorPageFilter:无法转发错误   请求页面[/ persons / no /]已作为回复   承诺。因此,响应可能具有错误的状态代码。   如果您的应用程序在WebSphere Application Server上运行,您可以   能够通过设置解决此问题   com.ibm.ws.webcontainer.invokeFlushAfterService为false

1 个答案:

答案 0 :(得分:1)

我找到this answer

@Bean
    public ErrorPageFilter errorPageFilter() {
        return new ErrorPageFilter();
    }

    @Bean
    public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }