如何使用 Spring 框架实现默认异常处理程序



我有一个 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

春季文档

这有效,但它捕获所有没有 ExceptionHandler 的异常。

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

最新更新