springboot全局异常处理学习

  1. 枚举项目错误信息

    public enum CommonError {
    
    	UNKOWN_ERROR("执行过程异常,请重试。"),
    	PARAMS_ERROR("非法参数"),
    	OBJECT_NULL("对象为空"),
    	QUERY_NULL("查询结果为空"),
    	REQUEST_NULL("请求参数为空");
    
    	private String errMessage;
    
    	public String getErrMessage() {
    		return errMessage;
    	}
    
    	CommonError(String errMessage) {
    		this.errMessage = errMessage;
    	}
    
    }
    
  2. 项目自定义业务异常

    /**
     * @version 1.0
     * @Author zhaozhixin
     * @Date 2023/4/16 23:12
     * @注释 本项目自定义异常类型
     */
    public class EducationOnlineException extends RuntimeException{
    
    
        public EducationOnlineException() {
        }
    
        private String message;
    
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public EducationOnlineException(String message) {
            super(message);
            this.message = message;
        }
    
        public static  void cast(CommonError commonError){
            throw new EducationOnlineException(commonError.getErrMessage());
        }
    
        public static  void cast(String message){
            throw new EducationOnlineException(message);
        }
    }
    
  3. 报错响应参数包装

    /**
     * @version 1.0
     * @Author zhaozhixin
     * @Date 2023/4/16 23:09
     * @注释 报错响应参数包装
     */
    public class RestErrorResponse implements Serializable {
    
    
        private String errMessage;
    
        public RestErrorResponse(String errMessage) {
            this.errMessage = errMessage;
        }
    
    
        public String getErrMessage() {
            return errMessage;
        }
    
        public void setErrMessage(String errMessage) {
            this.errMessage = errMessage;
        }
    }
    
  4. 全局异常处理类

    /**
     * @version 1.0
     * @Author zhaozhixin
     * @Date 2023/4/16 23:26
     * @注释 全局异常处理类
     */
    
    @Slf4j
    //@ControllerAdvice
    @RestControllerAdvice //相当于@ControllerAdvice + @ResponseBody
    public class GlobalExceptionHandler {
    
        //对项目的自定义异常类型进行处理(业务异常)
        @ExceptionHandler(EducationOnlineException.class)
        @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
        public RestErrorResponse  customException(EducationOnlineException onlineException){
    
            //记录异常日志
            log.error("业务异常{}",onlineException.getMessage(),onlineException);
            //解析出异常信息
            String message = onlineException.getMessage();
            RestErrorResponse restErrorResponse = new RestErrorResponse(message);
            return restErrorResponse;
        }
    
        //框架抛出的异常(系统异常)
        @ExceptionHandler(Exception.class)
        @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
        public RestErrorResponse  exception(Exception exception){
    
            //记录异常日志
            log.error("系统异常{}",exception.getMessage(),exception);
    
            //解析出异常信息
            RestErrorResponse restErrorResponse = new RestErrorResponse(CommonError.UNKOWN_ERROR.getErrMessage());
            return restErrorResponse;
        }
    }
    
    
  5. 在业务代码中的实例

    //参数的合法性校验
        if (StringUtils.isBlank(dto.getName())) {
            EducationOnlineException.cast(CommonError.PARAMS_ERROR);
        }
   //插入数据库
        int insert = courseBaseMapper.insert(courseBaseNew);
        if (insert <=0 ){
            EducationOnlineException.cast("添加课程失败");
        }
        
  //如果课程收费,价格没有填写也需要抛出异常
        if (charge.equals("201001")){
            if (courseMarketNew.getPrice()==null || courseMarketNew.getPrice() <= 0){
                EducationOnlineException.cast("课程的价格不能为空并且必须大于0");
            }
        }

你可能感兴趣的:(spring,boot,学习,java)