Spring统一异常处理
一、如何优雅的判定异常情况并抛异常?
1、用断言Assert(org.springframework.util.Assert)代替throw execption
用Assert.notNull代替对对象的null判断并抛出异常,其源码:
public static void notNull(@Nullable Object object, String message) { if (object == null) { throw new IllegalArgumentException(message); } }
我们也可以实现自己的Assert,抛出我们自定义的异常
2、Enum
自定义异常有两个属性,即code、message。这样的一对属性,可以使用枚举类
使用枚举类结合(继承)Assert,只需根据特定的异常情况定义不同的枚举实例,就能够针对不同情况抛出特定的异常(这里指携带特定的异常码和异常消息),这样既不用定义大量的异常类,同时还具备了断言的良好可读性
二、异常处理现状
1、controller层或者service层的 try {...} catch {...} finally {...} 代码块
2、Spring的@ExceptionHandler
其实际作用是:若在某个Controller类定义一个异常处理方法,并在方法上添加该注解,那么当出现指定的异常时,会执行该处理异常的方法,其可以使用springmvc提供的数据绑定,比如注入HttpServletRequest等,还可以接受一个当前抛出的Throwable对象。
但是,这样一来,就必须在每一个Controller类都定义一套这样的异常处理方法,因为异常可以是各种各样。这样一来,就会造成大量的冗余代码,而且若需要新增一种异常的处理逻辑,就必须修改所有Controller类了,很不优雅
三、Spring统一异常处理
1、@ControllerAdvice和@ExceptionHandler
注解@ControllerAdvice 、@RestControllerAdvice可以把异常处理器应用到所有控制器,而不是单个控制器。
借助该注解,我们可以实现:在独立的某个地方,比如单独一个类,定义一套对各种异常的处理机制,然后在类的签名加上注解@ControllerAdvice,统一对 不同阶段的、不同异常 进行处理。这就是统一异常处理的原理
定义统一异常处理器:
@Component @ControllerAdvice @ConditionalOnWebApplication @ConditionalOnMissingBean(UnifiedExceptionHandler.class) public class UnifiedExceptionHandler { /** * 生产环境 */ private final static String ENV_PROD = "prod"; @Autowired private UnifiedMessageSource unifiedMessageSource; /** * 当前环境 */ @Value("${spring.profiles.active}") private String profile; /** * 获取国际化消息 * * @param e 异常 * @return */ public String getMessage(BaseException e) { String code = "response." + e.getResponseEnum().toString(); String message = unifiedMessageSource.getMessage(code, e.getArgs()); if (message == null || message.isEmpty()) { return e.getMessage(); } return message; } /** * 业务异常 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler(value = BusinessException.class) @ResponseBody public ErrorResponse handleBusinessException(BaseException e) { log.error(e.getMessage(), e); return new ErrorResponse(e.getResponseEnum().getCode(), getMessage(e)); } /** * 自定义异常 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler(value = BaseException.class) @ResponseBody public ErrorResponse handleBaseException(BaseException e) { log.error(e.getMessage(), e); return new ErrorResponse(e.getResponseEnum().getCode(), getMessage(e)); } /** * Controller上一层相关异常 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler({ NoHandlerFoundException.class, HttpRequestMethodNotSupportedException.class, HttpMediaTypeNotSupportedException.class, MissingPathVariableException.class, MissingServletRequestParameterException.class, TypeMismatchException.class, HttpMessageNotReadableException.class, HttpMessageNotWritableException.class, // BindException.class, // MethodArgumentNotValidException.class HttpMediaTypeNotAcceptableException.class, ServletRequestBindingException.class, ConversionNotSupportedException.class, MissingServletRequestPartException.class, AsyncRequestTimeoutException.class }) @ResponseBody public ErrorResponse handleServletException(Exception e) { log.error(e.getMessage(), e); int code = CommonResponseEnum.SERVER_ERROR.getCode(); try { ServletResponseEnum servletExceptionEnum = ServletResponseEnum.valueOf(e.getClass().getSimpleName()); code = servletExceptionEnum.getCode(); } catch (IllegalArgumentException e1) { log.error("class [{}] not defined in enum {}", e.getClass().getName(), ServletResponseEnum.class.getName()); } if (ENV_PROD.equals(profile)) { // 当为生产环境, 不适合把具体的异常信息展示给用户, 比如404. code = CommonResponseEnum.SERVER_ERROR.getCode(); BaseException baseException = new BaseException(CommonResponseEnum.SERVER_ERROR); String message = getMessage(baseException); return new ErrorResponse(code, message); } return new ErrorResponse(code, e.getMessage()); } /** * 参数绑定异常 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler(value = BindException.class) @ResponseBody public ErrorResponse handleBindException(BindException e) { log.error("参数绑定校验异常", e); return wrapperBindingResult(e.getBindingResult()); } /** * 参数校验异常,将校验失败的所有异常组合成一条错误信息 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler(value = MethodArgumentNotValidException.class) @ResponseBody public ErrorResponse handleValidException(MethodArgumentNotValidException e) { log.error("参数绑定校验异常", e); return wrapperBindingResult(e.getBindingResult()); } /** * 包装绑定异常结果 * * @param bindingResult 绑定结果 * @return 异常结果 */ private ErrorResponse wrapperBindingResult(BindingResult bindingResult) { StringBuilder msg = new StringBuilder(); for (ObjectError error : bindingResult.getAllErrors()) { msg.append(", "); if (error instanceof FieldError) { msg.append(((FieldError) error).getField()).append(": "); } msg.append(error.getDefaultMessage() == null ? "" : error.getDefaultMessage()); } return new ErrorResponse(ArgumentResponseEnum.VALID_ERROR.getCode(), msg.substring(2)); } /** * 未定义异常 * * @param e 异常 * @return 异常结果 */ @ExceptionHandler(value = Exception.class) @ResponseBody public ErrorResponse handleException(Exception e) { log.error(e.getMessage(), e); if (ENV_PROD.equals(profile)) { // 当为生产环境, 不适合把具体的异常信息展示给用户, 比如数据库异常信息. int code = CommonResponseEnum.SERVER_ERROR.getCode(); BaseException baseException = new BaseException(CommonResponseEnum.SERVER_ERROR); String message = getMessage(baseException); return new ErrorResponse(code, message); } return new ErrorResponse(CommonResponseEnum.SERVER_ERROR.getCode(), e.getMessage()); } }
Spring统一异常处理(推荐使用)
@RestControllerAdvice、@ControllerAdvice:表示会处理所有controller中抛出的异常(如果已经自行catch那么则不会再处理了),默认会扫描指定包中所有@RequestMapping注解
@ExceptionHandler:通过@ExceptionHandler的 value 属性表示要处理哪个异常
Springboot的全局异常处理主要用到两个注解@ControllerAdvice和@ExceptionHandler,使用@ControllerAdvice注解的类是当前springboot程序中所有类的统一异常处理类,在该类中,使用@ExceptionHandler注解的方法来统一处理异常信息,该类对所有注解了@RequestMapping的控制器均有效
2、全局异常处理
使用全局异常处理器只需要两步:
实现HandlerExceptionResolver接口。
将实现类作为Spring Bean,这样Spring就能扫描到它并作为全局异常处理器加载
END.

浙公网安备 33010602011771号