第一步:自定义一个枚举类型规范异常信息(一般公司会有自己的枚举类。这里只是举例的信息)
@Getter public enum ExceptionEnum { INVALID_FILE_TYPE(400, "无效的文件类型!"), INVALID_PARAM_ERROR(400, "无效的请求参数!"), INVALID_PHONE_NUMBER(400, "无效的手机号码"), INVALID_VERIFY_CODE(400, "验证码错误!"), INVALID_USERNAME_PASSWORD(400, "无效的用户名和密码!"), INVALID_SERVER_ID_SECRET(400, "无效的服务id和密钥!"), INVALID_NOTIFY_PARAM(400, "回调参数有误!"), INVALID_NOTIFY_SIGN(400, "回调签名有误!"), CATEGORY_NOT_FOUND(404, "商品分类不存在!"), BRAND_NOT_FOUND(404, "品牌不存在!"), SPEC_NOT_FOUND(404, "规格不存在!"), GOODS_NOT_FOUND(404, "商品不存在!"), CARTS_NOT_FOUND(404, "购物车不存在!"), APPLICATION_NOT_FOUND(404, "应用不存在!"), ORDER_NOT_FOUND(404, "订单不存在!"), ORDER_DETAIL_NOT_FOUND(404, "订单数据不存在!"), DATA_TRANSFER_ERROR(500, "数据转换异常!"), INSERT_OPERATION_FAIL(500, "新增操作失败!"), UPDATE_OPERATION_FAIL(500, "更新操作失败!"), DELETE_OPERATION_FAIL(500, "删除操作失败!"), FILE_UPLOAD_ERROR(500, "文件上传失败!"), DIRECTORY_WRITER_ERROR(500, "目录写入失败!"), FILE_WRITER_ERROR(500, "文件写入失败!"), SEND_MESSAGE_ERROR(500, "短信发送失败!"), INVALID_ORDER_STATUS(500, "订单状态不正确!"), STOCK_NOT_ENOUGH_ERROR(500, "库存不足!"), UNAUTHORIZED(401, "登录失效或未登录!"); private int status; private String message; ExceptionEnum(int status, String message) { this.status = status; this.message = message; } }
第二步:自定义一个异常:
@Getter public class LyException extends RuntimeException {
private Integer status;
public LyException(Integer status, String message) { super(message); this.status = status; }
public LyException(ExceptionEnum exceptionEnum) { super(exceptionEnum.getMessage()); this.status = exceptionEnum.getStatus(); } }
第三步:自定义一个异常返回对象(不是异常,是返回对象,这里也是举例的信息)
@Getter public class LyExceptionResult {
private int status; private String message; private String timestamp;
public LyExceptionResult(LyException e) { this.status = e.getStatus(); this.message = e.getMessage(); this.timestamp = DateTime.now().toString("yyyy-MM-dd HH:mm:ss"); } }
第四步:自定义异常处理器
@ControllerAdvice注解 :默认情况下,会拦截所有加了@Controller 的类
@ControllerAdvice public class LyExceptionControllerAdvice {
/** * 拦截LyException */ @ExceptionHandler(LyException.class) public ResponseEntity<LyExceptionResult> handlerLyException(LyException e){ return ResponseEntity.status(e.getStatus()).body(new LyExceptionResult(e)); } }
|