当前位置: 首页 > news >正文

异常处理总结

自定义异常

​ 系统中的异常可以分为我们能预知的异常和未知的系统异常,对于我们能预知的异常如空值判断,用户名错误,密码错误等异常我们需要返回客户端,对于系统内部异常如SQL语法错误,参数格式转换错误等需要统一包装成友好的提示后再返回客户端,否则用户也看不懂系统内部的异常。

定义响应码ResponseCode ,方便之后的自定义异常

public enum ResponseCode {RESPONSE_CODE_200(200, "操作成功"),RESPONSE_CODE_400(400, "参数错误"),RESPONSE_CODE_1001(1001, "激活失败已过期"),RESPONSE_CODE_1002(1002, "密码不一致")private Integer code;private String message;ResponseCode(Integer code, String message) {this.code = code;this.message = message;}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}
}

定义已知异常BusinessException,用于区分项目中的已知异常和未知异常


public class BusinessException extends RuntimeException{private Integer code;public BusinessException(ResponseCode responseCode) {super(responseCode.getMessage());this.code = responseCode.getCode();}public Integer getCode() {return code;}public void setCode(Integer code) {this.code = code;}public BusinessException() {super();}public BusinessException(String s) {super(s);}public BusinessException(String message, Throwable cause) {super(message, cause);}public BusinessException(Throwable cause) {super(cause);}protected BusinessException(String message, Throwable cause,boolean enableSuppression,boolean writableStackTrace) {super(message, cause, enableSuppression, writableStackTrace);}
}

自定义断言工具类,避免大量if判断

public class AssertUtils {public static void isTrue(Boolean flag, ResponseCode responseCode) {if (!flag) {throw new BusinessException(responseCode);}}public static void isBlank(String str, ResponseCode responseCode) {if (StrUtil.isNotBlank(str)) {throw new BusinessException(responseCode);}}public static void isNotBlank(String str, ResponseCode responseCode) {if (StrUtil.isBlank(str)) {throw new BusinessException(responseCode);}}public static void isNull(Object object, ResponseCode responseCode) {if (Objects.nonNull(object)) {throw new BusinessException(responseCode);}}public static void isNotNull(Object object, ResponseCode responseCode) {if (Objects.isNull(object)) {throw new BusinessException(responseCode);}}public static void isNull(Collection collection, ResponseCode responseCode) {if (collection != null && !collection.isEmpty()) {throw new BusinessException(responseCode);}}public static void isNotNull(Collection collection, ResponseCode responseCode) {if (collection == null || collection.isEmpty()) {throw new BusinessException(responseCode);}}public static void isEq(String str1, String st2, ResponseCode responseCode) {if (!str1.equals(st2)) {throw new BusinessException(responseCode);}}public static void isEqIgnoreCase(String str1, String str2, ResponseCode responseCode) {if (!str1.equalsIgnoreCase(str2)) {throw new BusinessException(responseCode);}}public static void smallerThan(Long second, int i, ResponseCode responseCode) {if (second > i) {throw new BusinessException(responseCode);}}
}

全局异常处理类,不再写大量try - catch,由全局异常处理类自动捕获

@RestControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(BusinessException.class)public AjaxResult businessExceptionHandler(BusinessException e) {e.printStackTrace();return AjaxResult.me().setSuccess(false).setMessage(e.getMessage()).setCode(e.getCode());}//JSR-303校验所抛出的异常@ExceptionHandler(MethodArgumentNotValidException.class)public AjaxResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {e.printStackTrace();BindingResult bindingResult = e.getBindingResult();List<ObjectError> allErrors = bindingResult.getAllErrors();StringBuffer sb = new StringBuffer();allErrors.forEach(objectError -> sb.append(objectError.getDefaultMessage()).append("! "));return AjaxResult.me().setSuccess(false).setMessage(sb.toString()).setCode(ResponseCode.RESPONSE_CODE_400.getCode());}@ExceptionHandler(Exception.class)public AjaxResult ExceptionHandler(Exception e) {e.printStackTrace();return AjaxResult.me().setSuccess(false).setMessage(ResponseCode.RESPONSE_CODE_500.getMessage()).setCode(ResponseCode.RESPONSE_CODE_500.getCode());}
}

在使用dto接受前端参数时,可以使用JSR-303校验

@Data
public class PlaceOrderDTO {private String parentOrderNo;@NotNull(message = "请选择收货地址") // 当为空时会报错 -> "请选择收货地址"private OrderGiftAddress address;}

可以将异常信息定义在properties中在resources包下ValidationMessages.properties配置文件中,将中文转换为Unicode转义序列的UTF-16编码格式

example.error.blank     = \u4e0d\u80fd\u4e3a\u7a7a
\u4e0d 表示中文字符“不”。
\u80fd 表示中文字符“能”。
\u4e3a 表示中文字符“为”。
\u7a7a 表示中文字符“空”。

ValidationMessages.properties配置文件原本是在org.hibernate.validator包下的,因为javaapi中只定义了jsr303规范,具体实现是由其他包实现的,springboot-starter下是由org.hibernate.validator来实现的

@NotBlank(message = "${example.error.blank}")private String username;

在controller接口参数位置打上@Valid,JSR303才能生效

 @PostMapping("/placeorder")public AjaxResult placeOrder(@Valid @RequestBody PlaceOrderDTO dto) {orderGiftService.placeOrder(dto);return AjaxResult.me().setResultObj(dto.getUniPayOrderSn());}```
http://www.lryc.cn/news/376166.html

相关文章:

  • 大模型日报2024-06-18
  • NumPy 双曲函数与集合操作详解
  • ABSD-系统架构师(十三)
  • PLC通过Profibus协议转Modbus协议网关接LED大屏通讯
  • 第二十三篇——香农第二定律(二):到底要不要扁平化管理?
  • stm32f103 HAL库 HC-SR04测距
  • vue中通过自定义指令实现一个可拖拽,缩放的弹窗
  • FreeRtos-09事件组的使用
  • 多路h265监控录放开发-(1)建立head窗口并实现鼠标拖动整个窗口
  • ICMR 2024在普吉岛闭幕,学者与泰国舞者共舞,燃爆全场
  • 大模型精调:实现高效迁移学习的艺术
  • epoll服务端和客户端示例代码
  • 最大乘积和-第13届蓝桥杯省赛Python真题精选
  • 探索C嘎嘎的奇妙世界:第四关---引用与内联函数
  • DLS平台:惠誉全球经济展望——今年调增至2.6%,明年调减!
  • 数据结构习题
  • 交通银行软件开发工程师校招面试经历
  • bashrc和profile区别
  • BC153 [NOIP2010]数字统计
  • 浅谈LavelDB
  • Google Earth Engine(GEE)——NDVI的时间序列分析和在线出图
  • 谈吐的艺术(三)
  • pop链详细分析、构造(以[NISACTF 2022]babyserialize为例)
  • 使用超声波麦克风阵列预测数控机床刀具磨损
  • 怎么控制多个存储设备的访问权限?数据安全存储方案来了
  • 麒麟系统mate_indicators进程占用内存资源高
  • Docker高级篇之轻量化可视化工具Portainer
  • Vue32-挂载流程
  • 算法金 | 一个强大的算法模型:t-SNE !!
  • 用IAST工具强化“越权检测”能力,提升系统安全性