GlobalExceptionHandler.java
4.36 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
package com.glsk.exception;
import com.glsk.vo.RopResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 javax.servlet.http.HttpServletRequest;
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 ResponseEntity<RopResponse<?>> handleBusinessException(BusinessException e) {
log.error("业务异常: code={}, message={}", e.getCode(), e.getMessage(), e);
RopResponse<?> restResult = new RopResponse<>();
restResult.setCode(e.getCode().toString());
restResult.setMessage(e.getMessage());
return ResponseEntity
.status(HttpStatus.OK)
.body(restResult);
}
/**
* 参数校验异常统一处理
*/
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
public ResponseEntity<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<?> restResult = new RopResponse<>();
restResult.setMessage(e.getMessage());
return ResponseEntity
.status(HttpStatus.OK)
.body(restResult);
}
/**
* 处理请求方法不支持异常
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<RopResponse<?>> handleHttpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException e) {
log.error("请求方法不支持: {}", e.getMessage(), e);
RopResponse<?> restResult = new RopResponse<>();
restResult.setCode("405");
restResult.setMessage(e.getMessage());
return ResponseEntity
.status(HttpStatus.METHOD_NOT_ALLOWED)
.body(restResult);
}
/**
* 处理媒体类型不支持异常
*/
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseEntity<RopResponse<?>> handleHttpMediaTypeNotSupportedException(
HttpMediaTypeNotSupportedException e) {
log.error("媒体类型不支持: {}", e.getMessage(), e);
RopResponse<?> restResult = new RopResponse<>();
restResult.setCode("415");
restResult.setMessage(e.getMessage());
return ResponseEntity
.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
.body(restResult);
}
/**
* 处理其他所有异常
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<RopResponse<?>> handleException(Exception e, HttpServletRequest request) {
log.error("系统异常: {}", e.getMessage(), e);
RopResponse<?> restResult = new RopResponse<>();
restResult.setCode(ErrorCodeEnum.SYSTEM_ERROR.getCode().toString());
restResult.setMessage(ErrorCodeEnum.SYSTEM_ERROR.getMessage());
return ResponseEntity.status(HttpStatus.OK).body(restResult);
}
}