Java项目实战7——增强类

@TOC
在项目的建设过程中,我们需要统一一些公共部分的东西,比如返回体、异常处理、异常错误码等等。

定义异常码

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
37
38
package com.hao.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* 接口状态码枚举
*
* @author lihao
* @since 2022-7-4 21:08:56
**/
@Getter
@AllArgsConstructor
public enum StatusCodeEnum {

/**
* 成功
*/
SUCCESS(20000, "操作成功"),
/**
* 失败
*/
FAIL(51000, "操作失败"),
SYSTEM_ERROR(50000, "系统异常"),
VALID_ERROR(52000, "参数格式不正确");

/**
* 状态码
*/
private final Integer code;

/**
* 描述
*/
private final String desc;

}

JAVA

统一返回体

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.hao.vo;

import com.hao.enums.StatusCodeEnum;
import lombok.Data;

import static com.hao.enums.StatusCodeEnum.FAIL;
import static com.hao.enums.StatusCodeEnum.SUCCESS;


/**
* 接口返回类
*
* @author lihao
* @since 2022-7-4 21:07:10
*/
@Data
public class Result<T> {

/**
* 返回状态
*/
private Boolean flag;
/**
* 返回码
*/
private Integer code;
/**
* 返回信息
*/
private String message;
/**
* 返回数据
*/
private T data;

public static <T> Result<T> ok() {
return restResult(true, null, SUCCESS.getCode(), SUCCESS.getDesc());
}

public static <T> Result<T> ok(T data) {
return restResult(true, data, SUCCESS.getCode(), SUCCESS.getDesc());
}

public static <T> Result<T> ok(T data, String message) {
return restResult(true, data, SUCCESS.getCode(), message);
}

public static <T> Result<T> fail() {
return restResult(false, null, FAIL.getCode(), FAIL.getDesc());
}

public static <T> Result<T> fail(StatusCodeEnum statusCodeEnum) {
return restResult(false, null, statusCodeEnum.getCode(), statusCodeEnum.getDesc());
}

public static <T> Result<T> fail(String message) {
return restResult(false, message);
}

public static <T> Result<T> fail(T data) {
return restResult(false, data, FAIL.getCode(), FAIL.getDesc());
}

public static <T> Result<T> fail(T data, String message) {
return restResult(false, data, FAIL.getCode(), message);
}

public static <T> Result<T> fail(Integer code, String message) {
return restResult(false, null, code, message);
}

private static <T> Result<T> restResult(Boolean flag, String message) {
Result<T> apiResult = new Result<>();
apiResult.setFlag(flag);
apiResult.setCode(flag ? SUCCESS.getCode() : FAIL.getCode());
apiResult.setMessage(message);
return apiResult;
}

private static <T> Result<T> restResult(Boolean flag, T data, Integer code, String message) {
Result<T> apiResult = new Result<>();
apiResult.setFlag(flag);
apiResult.setData(data);
apiResult.setCode(code);
apiResult.setMessage(message);
return apiResult;
}

}

JAVA

统一业务异常类

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
37
38
39
40
package com.hao.advice;

import com.hao.enums.StatusCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;

import static com.hao.enums.StatusCodeEnum.FAIL;


/**
* 业务异常
*
* @author lihao
* @since 2022-7-4 21:11:22
*/
@Getter
@AllArgsConstructor
public class BizException extends RuntimeException {

/**
* 错误码
*/
private Integer code = FAIL.getCode();

/**
* 错误信息
*/
private final String message;

public BizException(String message) {
this.message = message;
}

public BizException(StatusCodeEnum statusCodeEnum) {
this.code = statusCodeEnum.getCode();
this.message = statusCodeEnum.getDesc();
}


}
JAVA

统一异常处理拦截器

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.hao.advice;

import com.hao.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

import static com.hao.enums.StatusCodeEnum.SYSTEM_ERROR;
import static com.hao.enums.StatusCodeEnum.VALID_ERROR;

/**
* @author lihao
*/
@Slf4j
@RestControllerAdvice
public class ControllerAdviceHandler {

/**
* 处理服务异常
*
* @param e 异常
* @return 接口异常信息
*/
@ExceptionHandler(value = BizException.class)
public Result<?> errorHandler(BizException e) {
return Result.fail(e.getCode(), e.getMessage());
}

/**
* 处理参数校验异常
*
* @param e 异常
* @return 接口异常信息
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result<?> errorHandler(MethodArgumentNotValidException e) {
return Result.fail(VALID_ERROR.getCode(), Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
}

/**
* 处理系统异常
*
* @param e 异常
* @return 接口异常信息
*/
@ExceptionHandler(value = Exception.class)
public Result<?> errorHandler(Exception e) {
e.printStackTrace();
return Result.fail(SYSTEM_ERROR.getCode(), SYSTEM_ERROR.getDesc());
}

}

JAVA

测试

测试返回体和异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

@GetMapping("/listAll")
@Operation(summary = "获取所有用户", description = "获取所有的用户信息,不分页")
public Result<List<User>> listAll() {
Result<List<User>> result = Result.ok();
result.setData(userService.list());
return result;
}

@GetMapping("/testException")
@Operation(summary = "测试异常", description = "异常测试,全局拦截器测试")
public Result<List<User>> testException() {
throw new BizException("系统崩了,出问题啦!!!!");
}

JAVA

返回体:

异常:

over,散会!!!


Java项目实战7——增强类
https://leehoward.cn/2022/07/04/Java项目实战7——增强类/
作者
lihao
发布于
2022年7月4日
许可协议