私人网站如何建,网站备案号位置,官方网站建设流程及费用,河北省邢台市介绍#xff1a;
在日常项目开发中#xff0c;异常是常见的#xff0c;但是如何更高效的处理好异常信息#xff0c;让我们能快速定位到BUG#xff0c;是很重要的#xff0c;不仅能够提高我们的开发效率#xff0c;还能让你代码看上去更舒服#xff0c;SpringBoot的项目…介绍
在日常项目开发中异常是常见的但是如何更高效的处理好异常信息让我们能快速定位到BUG是很重要的不仅能够提高我们的开发效率还能让你代码看上去更舒服SpringBoot的项目已经对有一定的异常处理了但是对于我们开发者而言可能就不太合适了因此我们需要对这些异常进行统一的捕获并处理。
我们只需在完整的springboot项目中添加四个java类即可
一异常枚举类
/*** version 1.0* Author guozhen8* Date 2023年11月23日 0023 17:57:01* 注释 异常返回枚举类*/
Getter
ToString
AllArgsConstructor
public enum RespBeanEnum {SUCCESS(200,SUCCESS),ERROR(500,服务端异常),BIND_ERROR(500210,参数校验异常),NULL_ERROR(500211,找不到数据),INSERT_ERROR(500212,数据插入异常-主键重复 );private final Integer code;private final String message;}
二异常返回结果类
/*** version 1.0* Author guozhen8* Date 2023年11月23日 0023 17:57:01* 注释 异常返回类*/
Data
NoArgsConstructor
AllArgsConstructor
public class RespBean {private long code;private String message;private Object obj;/*** 功能描述返回成功结果* param* return*/public static RespBean success(){return new RespBean(RespBeanEnum.SUCCESS.getCode(),RespBeanEnum.SUCCESS.getMessage(),null);}/*** 功能描述返回成功结果* param obj* return*/public static RespBean success(Object obj){return new RespBean(RespBeanEnum.SUCCESS.getCode(),RespBeanEnum.SUCCESS.getMessage(),obj);}/*** 功能描述返回失败结果* param respBeanEnum* return*/public static RespBean error(RespBeanEnum respBeanEnum){return new RespBean(respBeanEnum.getCode(),respBeanEnum.getMessage(),null);}/*** 功能描述返回失败结果* param respBeanEnum,obj* return*/public static RespBean error(RespBeanEnum respBeanEnum,Object obj){return new RespBean(respBeanEnum.getCode(),respBeanEnum.getMessage(),obj);}
}三全局异常
/*** version 1.0* Author guozhen8* Date 2023年11月23日 0023 17:57:01* 注释 全局异常*/
Data
NoArgsConstructor
AllArgsConstructor
public class GlobalException extends RuntimeException{private RespBeanEnum respBeanEnum;
}
四全局异常处理类
/*** version 1.0* Author guozhen8* Date 2023年11月23日 0023 17:57:01* 注释 自定义异常处理类*/
RestControllerAdvice
public class GlobalExceptionHandler {//调试日志private final Logger logger LoggerFactory.getLogger(this.getClass());ExceptionHandler(Exception.class)//处理哪些异常public RespBean ExceptionHandler(Exception e,HttpServletRequest request){//打印日志logger.error(Requst URL : {}Exception : {}, request.getRequestURL(),e);if(e instanceof GlobalException){//如果是之前自定义的异常GlobalException ex (GlobalException) e;return RespBean.error(ex.getRespBeanEnum());}else if(e instanceof BindException) { //没有通过参数校验注解抛出的异常BindException ce (BindException) e;RespBean respBean RespBean.error(RespBeanEnum.BIND_ERROR);respBean.setMessage(参数校验异常: ce.getMessage());return respBean;}else if(e instanceof DuplicateKeyException) { //插入重复数据抛出的异常DuplicateKeyException de (DuplicateKeyException) e;RespBean respBean RespBean.error(RespBeanEnum.INSERT_ERROR);respBean.setMessage(参数插入异常: de.getMessage());return respBean;}else if(e instanceof ConstraintViolationException) { //传入数据有误抛出的异常ConstraintViolationException de ( ConstraintViolationException) e;RespBean respBean RespBean.error(RespBeanEnum.BIND_ERROR);respBean.setMessage(参数校验异常: de.getMessage());return respBean;}return RespBean.error(RespBeanEnum.ERROR);}}
五使用方法
在使用时我们可以返回异常结果类或者直接抛出相关异常即可。
//1:返回异常结果
return RespBean.error(RespBeanEnum.ERROR,xxxx);
return RespBean.success(xxxx);
//2抛出异常
throw new BindException(xxxx);