SpringBoot 全局异常处理

SpringBoot 全局异常处理

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@Slf4j
@RestControllerAdvice
public class ExceptionController {

/**
* 校验错误拦截处理
*
* @param ex 异常
* @return 返回值
* StringBuilder sb = new StringBuilder();
* for (FieldError error : ex.getBindingResult().getFieldErrors()) {
* sb.append(error.getDefaultMessage()).append(";");
* }
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ApiMessage<Object> methodArgumentNotValidHandler(MethodArgumentNotValidException ex) {
//按需重新封装需要返回的错误信息
List<ArgumentInvalid> invalidArguments = new ArrayList<>();
//解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
ex.getBindingResult().getFieldErrors().forEach(fieldError -> invalidArguments.add(new ArgumentInvalid(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage())));
return new ApiMessage<>(ExceptionCode.PARAMETER_ERROR, invalidArguments);
}

@ExceptionHandler(value = BindException.class)
public ApiMessage<Object> bindExceptionHandler(BindException ex) {
//按需重新封装需要返回的错误信息
List<ArgumentInvalid> invalidArguments = new ArrayList<>();
//解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
ex.getBindingResult().getFieldErrors().forEach(fieldError -> invalidArguments.add(new ArgumentInvalid(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage())));
return new ApiMessage<>(ExceptionCode.PARAMETER_ERROR, invalidArguments);
}

/**
* HTTP请求方式不正确
*
* @param ex 异常
* @return 返回值
*/
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
public ApiMessage<Object> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
log.error("HTTP请求方式不正确:【{}】", ex.getMessage());
return new ApiMessage<>(ex);
}

/**
* 请求参数不全
*
* @param ex 异常
* @return 返回值
*/
@ExceptionHandler(value = MissingServletRequestParameterException.class)
public ApiMessage<Object> missingServletRequestParameterException(MissingServletRequestParameterException ex) {
log.error("请求参数不全:【{}】", ex.getMessage());
return new ApiMessage<>(ex);
}

/**
* 请求参数类型不正确
*
* @param ex 异常
* @return 返回值
*/
@ExceptionHandler(value = TypeMismatchException.class)
public ApiMessage<Object> typeMismatchException(TypeMismatchException ex) {
log.error("请求参数类型不正确:【{}】", ex.getMessage());
return new ApiMessage<>(ex);
}

/**
* 数据格式不正确
*
* @param ex 异常
* @return 返回值
*/
@ExceptionHandler(value = DataFormatException.class)
public ApiMessage<Object> dataFormatException(DataFormatException ex) {
log.error("数据格式不正确:【{}】", ex.getMessage());
return new ApiMessage<>(ex);
}

/**
* 非法输入或断言错误
*
* @param ex 异常
* @return 返回值
*/
@ExceptionHandler(value = IllegalArgumentException.class)
public ApiMessage<Object> illegalArgumentException(IllegalArgumentException ex) {
log.error("非法输入或断言错误:【{}】", ex.getMessage());
return new ApiMessage<>(ex);
}

/**
* 请求参数错误
*
* @param ex 异常
* @return 返回值
*/
@ExceptionHandler(value = ConstraintViolationException.class)
public ApiMessage<Object> constraintViolationException(ConstraintViolationException ex) {
log.error("请求参数错误:【{}】", ex.getMessage());
return new ApiMessage<>(ex);
}

/**
* 操作数据库出现异常
*
* @param ex 异常
* @return 返回值
*/
@ExceptionHandler(value = DataAccessException.class)
public ApiMessage<Object> dataDoException(DataAccessException ex) {
log.error("操作数据库出现异常:【{}】", ex.getMessage());
return new ApiMessage<>(ex);
}

/**
* 系统异常
*
* @param ex 异常
* @return 返回值
*/
@ExceptionHandler(Exception.class)
public ApiMessage<Object> apiExceptionHandler(Exception ex) {
//只打印15行的错误堆栈
int count = 1;
StringBuilder sb = new StringBuilder();
for (StackTraceElement stackTraceElement : ex.getStackTrace()) {
sb.append(stackTraceElement.toString());
// if (count++ >= 30) {
// break;
// }
sb.append("\n");
}
log.error("系统异常:【{}】", sb.toString());
return new ApiMessage<>(ex);
}

/**
* 自定义异常
*
* @param apiException 自定义异常
* @return 返回值
*/
@ExceptionHandler(ApiException.class)
public ApiMessage<Object> apiException(ApiException apiException) {
return new ApiMessage<>(apiException);
}

@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
static class ArgumentInvalid {
/**
* 字段
*/
private String field;
/**
* 字段值
*/
private Object rejectedValue;
/**
* 默认值
*/
private String defaultMessage;
}
}

SpringBoot 全局异常处理
https://happyloves.cn/20221123/f2126e09480c.html
作者
赵小胖
发布于
2022年11月23日
许可协议