GlobalExceptionHandler.java
4.84 KB
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
package com.glsk.exception;
import com.glsk.vo.RopResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
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 java.util.Optional;
import java.util.stream.Collectors;
/**
* @Description 本地全局异常处理类(集成TraceId版本)
* @Author yelq
* @Date 2025/9/19-20:47
* @Version 1.0
*/
@Order(9999)
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
public GlobalExceptionHandler() {
}
/**
* 处理业务异常
*/
@ExceptionHandler(BusinessException.class)
public RopResponse<?> handleBusinessException(BusinessException e) {
log.error("业务异常: code={}, message={}", e.getCode(), e.getMessage(), e);
RopResponse<RopResponse<?>> restResult = new RopResponse<>();
restResult.setCode("0");
restResult.setMessage("调用服务成功!");
RopResponse<?> exceptionMessage = new RopResponse<>();
exceptionMessage.setCode(e.getCode().toString());
exceptionMessage.setMessage(e.getMessage());
restResult.setData(exceptionMessage);
return restResult;
}
/**
* 参数校验异常统一处理
*/
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
public RopResponse<?> handleValidationException(Exception e) {
BindingResult bindingResult = null;
if (e instanceof MethodArgumentNotValidException) {
bindingResult = ((MethodArgumentNotValidException) e).getBindingResult();
} else if (e instanceof BindException) {
bindingResult = ((BindException) e).getBindingResult();
}
String message = Optional.ofNullable(bindingResult)
.map(result -> result.getAllErrors().stream()
.map(ObjectError::getDefaultMessage)
.collect(Collectors.joining("; ")))
.orElse("参数校验失败");
log.error("参数校验异常: {}", message, e);
RopResponse<RopResponse<?>> restResult = new RopResponse<>();
restResult.setCode("0");
restResult.setMessage("调用服务成功!");
RopResponse<?> exceptionMessage = new RopResponse<>();
exceptionMessage.setMessage(e.getMessage());
restResult.setData(exceptionMessage);
return restResult;
}
/**
* 处理请求方法不支持异常
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public RopResponse<?> handleHttpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException e) {
log.error("请求方法不支持: {}", e.getMessage(), e);
RopResponse<RopResponse<?>> restResult = new RopResponse<>();
restResult.setCode("0");
restResult.setMessage("调用服务成功!");
RopResponse<?> exceptionMessage = new RopResponse<>();
exceptionMessage.setCode("405");
exceptionMessage.setMessage("请求方法不支持");
restResult.setData(exceptionMessage);
return restResult;
}
/**
* 处理媒体类型不支持异常
*/
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public RopResponse<?> handleHttpMediaTypeNotSupportedException(
HttpMediaTypeNotSupportedException e) {
log.error("媒体类型不支持: {}", e.getMessage(), e);
RopResponse<RopResponse<?>> restResult = new RopResponse<>();
restResult.setCode("0");
restResult.setMessage("调用服务成功!");
RopResponse<?> exceptionMessage = new RopResponse<>();
exceptionMessage.setCode("415");
exceptionMessage.setMessage("媒体类型不支持");
restResult.setData(exceptionMessage);
return restResult;
}
/**
* 处理其他所有异常
*/
@ExceptionHandler(Exception.class)
public RopResponse<?> handleException(Exception e) {
log.error("系统异常: {}", e.getMessage(), e);
RopResponse<RopResponse<?>> restResult = new RopResponse<>();
restResult.setCode("0");
restResult.setMessage("调用服务成功!");
RopResponse<?> exceptionMessage = new RopResponse<>();
exceptionMessage.setCode(ErrorCodeEnum.SYSTEM_ERROR.getCode().toString());
exceptionMessage.setMessage(ErrorCodeEnum.SYSTEM_ERROR.getMessage());
restResult.setData(exceptionMessage);
return restResult;
}
}