2015-04-14 45 views
0

我有一個GlobalExceptionhandler:如何用Spring框架實現Default Exception Handler?

@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

回答

0

這工作,但它捕獲沒有的ExceptionHandler所有異常。

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