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
|
package com.example.demo.exception;
import com.example.demo.common.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 全局异常处理器
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
/**
* 处理自定义业务异常
*/
@ExceptionHandler(BusinessException.class)
public ApiResponse<?> handleBusinessException(BusinessException e) {
logger.error("业务异常: {}", e.getMessage());
return ApiResponse.fail(e.getCode(), e.getMessage());
}
/**
* 处理参数校验异常(MethodArgumentNotValidException)
* 用于校验 @RequestBody 注解的参数
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ApiResponse<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
Map<String, String> errors = new HashMap<>();
for (FieldError error : e.getBindingResult().getFieldErrors()) {
errors.put(error.getField(), error.getDefaultMessage());
}
logger.error("参数校验失败: {}", errors);
return ApiResponse.fail(40002, "参数校验失败", errors);
}
/**
* 处理参数绑定异常(BindException)
* 用于校验 @ModelAttribute 注解的参数
*/
@ExceptionHandler(BindException.class)
public ApiResponse<?> handleBindException(BindException e) {
Map<String, String> errors = new HashMap<>();
for (FieldError error : e.getBindingResult().getFieldErrors()) {
errors.put(error.getField(), error.getDefaultMessage());
}
logger.error("参数绑定失败: {}", errors);
return ApiResponse.fail(40003, "参数绑定失败", errors);
}
/**
* 处理请求参数格式错误(MethodArgumentTypeMismatchException)
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ApiResponse<?> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
logger.error("参数类型不匹配: {}", e.getMessage());
return ApiResponse.fail(40004, "参数类型不匹配: " + e.getName());
}
/**
* 处理请求体解析错误(HttpMessageNotReadableException)
*/
@ExceptionHandler(HttpMessageNotReadableException.class)
public ApiResponse<?> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
logger.error("请求体解析失败: {}", e.getMessage());
return ApiResponse.fail(40005, "请求体格式错误");
}
/**
* 处理方法不支持异常(HttpRequestMethodNotSupportedException)
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ApiResponse<?> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
logger.error("不支持的请求方法: {}", e.getMessage());
return ApiResponse.fail(40006, "不支持的请求方法: " + e.getMethod());
}
/**
* 处理约束违反异常(ConstraintViolationException)
* 用于校验 @PathVariable 和 @RequestParam 注解的参数
*/
@ExceptionHandler(ConstraintViolationException.class)
public ApiResponse<?> handleConstraintViolationException(ConstraintViolationException e) {
Map<String, String> errors = new HashMap<>();
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
for (ConstraintViolation<?> violation : violations) {
String propertyPath = violation.getPropertyPath().toString();
String message = violation.getMessage();
errors.put(propertyPath, message);
}
logger.error("参数约束校验失败: {}", errors);
return ApiResponse.fail(40007, "参数约束校验失败", errors);
}
/**
* 处理未定义的其他异常
*/
@ExceptionHandler(Exception.class)
public ApiResponse<?> handleException(Exception e) {
logger.error("系统内部异常", e);
return ApiResponse.fail(50000, "系统内部异常,请联系管理员");
}
}
|