zoukankan      html  css  js  c++  java
  • SpringBoot 通用Error设计

    在项目中需要设计统一的错误消息,通常使用枚举类定义“错误码”与“错误消息”;
    并且也可以做错误消息自定义。

    定义通过错误接口类:CommonError

    public interface CommonError {
    
        // 获取错误码
        int getErrorCode();
    
        // 获取错误消息
        String getErrorMsg();
    
        // 设置错误消息,用于覆盖Enumeration中设置的默认错误消息
        CommonError setErrorMsg(String errorMsg);
    }
    

    定义错误枚举类:EnumError

    /**
     * 继承至CommonError
     */
    public enum EnumError implements CommonError {
    
        // 10000开头为通用错误类型
        UNKNOWN_ERROR(10001, "未知错误"),
        PARAMETER_VALIDATION_ERROR(10002, "参数不合法"),
    
        // 20000开头为用户信息相关错误码定义
        USER_NOT_EXIST(20001, "用户不存在"),
        USER_LOGIN_FAIL(20002, "用户名或密码错误"),
        USER_NOT_LOGIN(20003, "用户未登录"),
    
        // 定义错误码和错误消息字段
        private int errorCode;
        private String errorMsg;
    
        EnumError(int errorCode, String errorMsg) {
            this.errorCode = errorCode;
            this.errorMsg = errorMsg;
        }
    
        @Override
        public int getErrorCode() {
            return errorCode;
        }
    
        @Override
        public String getErrorMsg() {
            return errorMsg;
        }
    
        // 主要的作用:可以修改错误码对应的errorMessage
        @Override
        public CommonError setErrorMsg(String errorMsg) {
            this.errorMsg = errorMsg;
            return this;
        }
    }
    

    定义业务异常类:BusinessException

    /**
     * 包装器业务异常类实现模式
     */
    public class BusinessException extends Exception implements CommonError {
    
        private CommonError commonError;
    
        // 通用构造器
        public BusinessException(CommonError commonError){
            super();
            this.commonError = commonError;
        }
    
        // 覆盖ErrorMessage的构造器
        public BusinessException(CommonError commonError, String errorMsg){
            super();
            this.commonError = commonError;
            this.commonError.setErrorMsg(errorMsg);
        }
    
        @Override
        public int getErrorCode() {
            return this.commonError.getErrorCode();
        }
    
        @Override
        public String getErrorMsg() {
            return this.commonError.getErrorMsg();
        }
    
        // 覆盖默认的errorMessage
        @Override
        public CommonError setErrorMsg(String errorMsg) {
            this.commonError.setErrorMsg(errorMsg);
            return this;
        }
    }
    

    在Controller中使用BusinessException:定义BaseController类

    public class BaseController {
    
        // 定义ExceptionHandler解决未被Controller层吸收的exception
        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public CommonReturnType exceptionHandler(Exception ex){
            Map<String, Object> responseData = new HashMap<>();
            if(ex instanceof BusinessException){
                BusinessException businessException = (BusinessException)ex;
                responseData.put("errorCode", businessException.getErrorCode());
                responseData.put("errorMsg", businessException.getErrorMsg());
            } else {
                responseData.put("errorCode", EnumBusinessError.UNKNOWN_ERROR.getErrorCode());
                responseData.put("errorMsg", EnumBusinessError.UNKNOWN_ERROR.getErrorMsg());
            }
    
            return CommonReturnType.create(responseData, "failed");
        }
    }
    

    在UserController中实现业务逻辑

    // 用户登录接口
        @RequestMapping(value = "/login", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")
        @ResponseBody
        public CommonReturnType login(@RequestParam(name = "username") String username,
                                      @RequestParam(name = "password") String password) throws BusinessException {
            // 第一步,入参校验
            if(StringUtils.isEmpty(username) || StringUtils.isEmpty(username)){
                throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR, "用户名和密码不能为空");
            }
    
            // 第二步,校验用户登录是否合法
            UserModel userModel = userService.validateLogin(username, PasswordEncrypt.encodeByMd5(password));
    
            // 第三步,加入到用户登录后的Session中
            this.httpServletRequest.getSession().setAttribute("IS_LOGIN", true);
            this.httpServletRequest.getSession().setAttribute("LOGIN_USER", userModel);
    
            return CommonReturnType.create(null);
        }
    
  • 相关阅读:
    Go语言踩过的坑---记录GOPATH在GOLAND中的坑
    反射小例子
    制作pip包
    mac常用软件安装链接
    YCSB压测elasticsearch
    SSO和Auth2.0
    JAVA内部类的四大作用
    修改fastadmin,添加模糊查询
    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message
    taro3.x: 封装实现chat emit on
  • 原文地址:https://www.cnblogs.com/vincenshen/p/10422064.html
Copyright © 2011-2022 走看看