迷新白的博客 迷新白的博客
首页
随笔
  • Vuepress
  • Springboot
  • 开发工具
  • 系统工具
读吧
  • 智能浇花系统 (opens new window)
  • 用户中心系统 (opens new window)
  • 关于
  • 友情链接
GitHub (opens new window)

迷新白

愿你平安
首页
随笔
  • Vuepress
  • Springboot
  • 开发工具
  • 系统工具
读吧
  • 智能浇花系统 (opens new window)
  • 用户中心系统 (opens new window)
  • 关于
  • 友情链接
GitHub (opens new window)
  • 用户中心系统

    • 前端初始化
    • 用户中心后端-1
    • 用户中心后端-2
    • 注册模块(后端)
    • 登录模块(后端)
    • 管理模块(后端)
    • Ant Design Pro前端初始化
    • 登录+注册模块(前端)
    • 管理模块+登录状态(前端)
    • 注销模块+校验模块(前后端)
    • 异常处理器(后端优化)
      • 后端优化
        • 自定义异常及其错误代码(debug中)
        • 全局异常处理器
        • 测试
        • 定义全局异常处理器
      • 推送至github
    • 请求响应处理器(前端)
  • 仿Deepseek官网AI聊天网站

  • 尤克里里音月-Flutter(需求分析阶段)

  • 项目
  • 用户中心系统
迷新白
2025-04-12
目录

异常处理器(后端优化)

# 12.异常处理器(后端优化)

继续做后端优化

# 后端优化

# 自定义异常及其错误代码(debug中)

  1. common包下新建ErrorCode.java(Enum枚举类型)image-20250412112230106

    • 补充代码
    package com.msingbai.usercenter.common;
    
    /**
     * 错误码
     */
    public enum ErrorCode {
        SUCCESS(0, "success", ""),
        PARAMS_ERROR(4000, "请求参数错误", ""),
        NULL_ERROR(4001, "请求参数为空", ""),
        NO_LOGIN(40100, "未登录", ""),
        NO_AUTH(40101, "无权限", "");
    
        private final int code;
        /**
         * 状态码信息
         */
        private final String message;
        /**
         * 状态码描述(详情)
         */
        private final String description;
    
        ErrorCode(int code, String message, String description) {
            this.code = code;
            this.message = message;
            this.description = description;
        }
        
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    • 按Alt+Insert选择Getterimage-20250412112900921

    • 生成了下面的代码

          public String getMessage() {
              return message;
          }
      
          public String getDescription() {
              return description;
          }
      
          public int getCode() {
              return code;
          }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
  2. 修改BaseResponse

    package com.msingbai.usercenter.common;
    
    import lombok.Data;
    
    import java.io.Serializable;
    
    @Data
    public class BaseResponse <T> implements Serializable {
    
        private int code;
    
        private T data;
    
        private String message;
    
        private String description;
    
        public BaseResponse(int code, T data, String message, String description) {
            this.code = code;
            this.data = data;
            this.message = message;
            this.description = description;
        }
    
        public BaseResponse(int code, T data, String message) {
            this(code, data, "","");
        }
    
        public BaseResponse(int code, T data) {
            this(code, data, "", "");
        }
    
        public BaseResponse(ErrorCode errorCode) {
            this(errorCode.getCode(),null,errorCode.getMessage(),errorCode.getDescription());
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
  3. 完善ResultUtils

    package com.msingbai.usercenter.common;
    
    /**
     * 返回工具类
     *
     */
    public class ResultUtils {
    
        /**
         * 成功
         *
         * @param data
         * @return
         * @param <T>
         */
        public static <T> BaseResponse<T> success(T data) {
            return new BaseResponse<>(0,data,"success");
        }
    
        /**
         * 失败
         *
         * @param errorCode
         * @return
         * @param <T>
         */
        public static <T> BaseResponse<T> error(ErrorCode errorCode) {
            return new BaseResponse<>(errorCode);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
  4. UserController.java

       .....
       @PostMapping("/register")
        public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
            if (userRegisterRequest == null) {
                return ResultUtils.error(ErrorCode.PARAMS_ERROR);
            }
            .....
                @PostMapping("/login")
        public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest,HttpServletRequest request) {
            if (userLoginRequest == null) {
                return ResultUtils.error(ErrorCode.PARAMS_ERROR);
            }
            String userAccount = userLoginRequest.getUserAccount();
            String userPassword = userLoginRequest.getUserPassword();
            if (StringUtils.isAnyBlank(userAccount, userPassword)) {
                return ResultUtils.error(ErrorCode.PARAMS_ERROR);
            }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

# 全局异常处理器

  1. com.xxxx.usercenter新建exception包,exception包下新建BusinessException.java image-20250412114751763image-20250412114958947

  2. 选择code和descriptionimage-20250412114958947

  3. 增加下面这段代码,继续生成Getter

        public BusinessException(ErrorCode errorCode) {
            super(errorCode.getMessage());
            this.code = errorCode.getCode();
            this.description = errorCode.getDescription();
        }
    
        public BusinessException(ErrorCode errorCode, String description) {
            super(errorCode.getMessage());
            this.code = errorCode.getCode();
            this.description = description;
        }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
  4. 增加final字段image-20250412115426047

# 测试

输入一个错误的登录密码 返回image-20250412115709758

# 定义全局异常处理器

  • exception包下新建GlobalExceptionHandler.javaimage-20250412120613768

  • ErrorCode中新增系统内部异常状态码image-20250412120701968

  • ResultUtils中新增三个构造函数

        /**
         * 失败
         * @param code
         * @param message
         * @param description
         * @return
         */
        public static BaseResponse error(int code, String message, String description) {
            return new BaseResponse(code,null,message,description);
        }
    
        /**
         * 失败
         * @param errorCode
         * @param message
         * @param description
         * @return
         */
        public static BaseResponse error(ErrorCode errorCode, String message, String description) {
            return new BaseResponse(errorCode.getCode(),null,message,description);
        }
    
        /**
         * 失败
         * @param errorCode
         * @param description
         * @return
         */
        public static BaseResponse error(ErrorCode errorCode, String description) {
            return new BaseResponse(errorCode.getCode(),errorCode.getMessage(),description);
        }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
  • GlobalExceptionHandler中编写具体的异常处理代码image-20250412123250367

  • 修改UserControllerimage-20250412121902669register


    image-20250412121959221search


    image-20250412122144027current


    image-20250412122208309delete


    image-20250412122228723logout


    image-20250412122314043login

  • 修改UserServiceImplimage-20250412122736961


    image-20250412122813827

  • 测试,登录功能运行异常image-20250412124502655

  • Debug

    • 这个功能问题很多,技术力不足,不做了
    • 2025/4/22 继续做

在登录时 我输入了错误的密码 返回的错误类型是不存在,不符合预期

image-20250422151137629

问题在于这段 image-20250422151435494

查询条件不应该同时包含密码,如果输入错误的密码,也会返回找不到用户的错误,应该只根据账户进行查询,然后再验证密码。

改成如下逻辑

/* 2.加密 */
String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
//账户不能重复
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userAccount", userAccount);
User user = userMapper.selectOne(queryWrapper);
//用户不存在
if (user == null){
    throw new BusinessException(ErrorCode.NULL_ERROR,"用户不存在");
}
// 4. 验证密码
if (!encryptPassword.equals(user.getUserPassword())) {
    throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码错误");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

image-20250422152022171

错误已经正常显示了

# 推送至github

文字写于:广东

#用户中心系统
更新时间: 2025/4/25 20:22:48
注销模块+校验模块(前后端)
请求响应处理器(前端)

← 注销模块+校验模块(前后端) 请求响应处理器(前端)→

最近更新
01
第一次对话完善
04-27
02
保留上下文对话
04-27
03
首页完善(后端)
04-27
更多文章>
Theme by Vdoing | Copyright © 2022-2025 迷新白 | 的博客
sitemap icon by Icons8
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式