如何使用Spring框架实现Default Exception Handler?

时间:2015-04-14 19:07:29

标签: java spring spring-mvc exception exception-handling

我有一个GlobalExceptionhandler:

@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(FooException.class)
    public ModelAndView alreadyVerifiedExceptionHandler(FooException ex) {
        ModelAndView response = new ModelAndView();
        response.addObject("status", ex.getStatus());
        response.addObject("errorCode", ex.getErrorCode());
        response.addObject("errorMessage", ex.getErrorMessage());
        return response;
    }
    .....
}

我有几个ExceptionHandler,默认情况下我会有一个ExceptionHandler。怎么办?

春季版:3.2.3

Spring Doc

1 个答案:

答案 0 :(得分:0)

这样可行,但它会捕获所有没有ExceptionHandler的异常。

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = {Exception.class, RuntimeException.class})
public ModelAndView defaultErrorHandler(Exception ex) {
...
}