用户中心后端-7
# 12.用户中心后端-7
继续做后端优化
# 后端优化
# 自定义异常及其错误代码(❌这个功能bug很多,不要做)
common包下新建ErrorCode.java(Enum枚举类型)
- 补充代码
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选择Getter
生成了下面的代码
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
修改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完善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
30UserController.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
# 全局异常处理器
com.xxxx.usercenter
新建exception
包,exception
包下新建BusinessException.java
选择code和description
增加下面这段代码,继续生成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增加final字段
# 测试
输入一个错误的登录密码 返回
# 定义全局异常处理器
exception
包下新建GlobalExceptionHandler.java
ErrorCode中新增系统内部异常状态码
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
31GlobalExceptionHandler中编写具体的异常处理代码
修改UserController
register
search
current
delete
logout
login
修改UserServiceImpl
测试,登录功能运行异常
Debug
- 这个功能问题很多,技术力不足,不做了
# 推送至github
文字写于:广东
更新时间: 2025/4/15 05:58:52