@RestControllerAdvice 注解的使用
1、作用
此注解通过对异常的拦截实现接口的统一返回格式。
2、使用方式
首先定义返回对象ResponseDto
1 @Data 2 public class ResponseDto<T> implements Serializable { 3 private static final long serialVersionUID = -284719732991678911L; 4 5 private String code; 6 7 private String message; 8 9 private T data; 10 11 public static <T> ResponseDto<T> success() { 12 ResponseDto<T> responseDto = new ResponseDto<>(); 13 responseDto.setCode(“1”)); 14 responseDto.setMessage(“ok”); 15 return responseDto; 16 } 17 18 public static <T> ResponseDto<T> success(T data) { 19 ResponseDto<T> responseDto = new ResponseDto<>(); 20 responseDto.setCode(“1”)); 21 responseDto.setMessage(“ok”); 22 responseDto.setDate(data); 23 return responseDto; 24 } 25 26 public static <T> ResponseDto<T> fail(ResponseCodeEnum data) { 27 ResponseDto<T> responseDto = new ResponseDto<>(); 28 responseDto.setCode(data.FAILURE.getCode()); 29 responseDto.setMessage(data.FAILURE.getMessage()); 30 return responseDto; 31 } 32 33 }
然后定义返回码的枚举类
1 public enum ResponseCodeEnum { 2 SUCCESS("0", "成功"), 3 FAILURE("-1", "系统异常"); 4 5 private String code; 6 private String message; 7 }
自定义的异常类
1 @Data 2 public class BusinessException extends RuntimeException{ 3 private String code; 4 private String message; 5 public BusinessException () {} 6 7 public BusinessException (Exception e) { 8 super(e); 9 } 10 11 public BusinessException (String code, String message) { 12 super(message); 13 this.code = code; 14 this.message = message; 15 } 16 17 public BusinessException (ResponseCodeEnum codeEnum) { 18 super(codeEnum.getMessage()); 19 this.code = codeEnum.getCode(); 20 this.message = codeEnum.getMessage(); 21 } 22 }
关键的@RestControllerAdvice
1 @RestControllerAdvice 2 @Component 3 public class GlobalExceptionHandler { 4 private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); 5 6 @ExceptionHandler(Exception.class) 7 public ResponseDto handlerCommonException (Exception e) { 8 ResponseDto responseDto = new ResponseDto<>(); 9 responseDto.setCode(ResponseCodeEnum.FAILURE.getCode()); 10 responseDto.setMessage(ResponseCodeEnum.FAILURE.getMessage()); 11 logger.info("GlobalExceptionHandler.handlerCommonException exception:" + e); 12 return responseDto; 13 } 14 // 报StaffPointException时,对其进行拦截并处理的方法 15 @ExceptionHandler(BusinessException.class) 16 public ResponseDto handlerCustomizeException (BusinessException e) { 17 ResponseDto responseDto = new ResponseDto<>(); 18 responseDto.setCode(e.getCode()); 19 responseDto.setMessage(e.getMessage()); 20 logger.info("GlobalExceptionHandler.handlerCustomizeException BusinessException:" + e); 21 return responseDto; 22 } 23 }
补充
一个项目中也可以同时定义多套ExceptionHandler
代码如下:
1 @RestControllerAdvice(basePackageClasses = {com.cn.controller.ApiController.class}) 2 @Order(Ordered.HIGHEST_PRECEDENCE) 3 public class ApiExceptionHandler { 4 5 ... 6 }
@RestControllerAdvice注解使用了basePackageClasses指定了类,说明此ExceptionHandler只作用到这个Controller,对其他无效。
@Order(Ordered.HIGHEST_PRECEDENCE) 顺序注解,要提高此ExceptionHandler的执行顺序,必须在全局的GlobalExceptionHandler之前执行,如果此ExceptionHandler定义的异常未拦截成功,在走GlobalExceptionHandler
莫听穿林打叶声,何妨吟啸且徐行!!!