|
3 | 3 | import lazy.fast.code.core.web.result.MsgEnum;
|
4 | 4 | import lazy.fast.code.core.web.result.ResultMsg;
|
5 | 5 | import lombok.extern.slf4j.Slf4j;
|
6 |
| -import org.springframework.beans.TypeMismatchException; |
7 | 6 | import org.springframework.http.HttpStatus;
|
8 | 7 | import org.springframework.http.ResponseEntity;
|
9 | 8 | import org.springframework.validation.BindException;
|
|
16 | 15 | import org.springframework.web.bind.MissingServletRequestParameterException;
|
17 | 16 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
18 | 17 | import org.springframework.web.client.RestClientException;
|
| 18 | +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
19 | 19 |
|
20 | 20 | import java.util.List;
|
21 | 21 |
|
22 | 22 | /**
|
23 | 23 | * 全局异常处理器 - 只针对返回JSON数据交互格式(标记为@RestController类) <br />
|
24 | 24 | * 应用应该创建一个自定义全局异常处理器,并继承此类,让此类定义的ExceptionHandler生效,如:
|
| 25 | + * |
25 | 26 | * <pre>
|
26 | 27 | * @RestControllerAdvice
|
27 | 28 | * public class GlobalRestExceptionHandler extends AbstractRestExceptionHandler {
|
@@ -86,10 +87,11 @@ public ResponseEntity<ResultMsg> illegalStateExceptionHandler(IllegalStateExcept
|
86 | 87 | return ResponseEntity.badRequest().body(ResultMsg.fail(e.getParameterName() + "参数缺失"));
|
87 | 88 | }
|
88 | 89 |
|
89 |
| - @ExceptionHandler(value = TypeMismatchException.class) |
90 |
| - public ResponseEntity<ResultMsg> typeMismatchExceptionHandler(TypeMismatchException e) { |
| 90 | + @ExceptionHandler(value = MethodArgumentTypeMismatchException.class) |
| 91 | + public ResponseEntity<ResultMsg> typeMismatchExceptionHandler(MethodArgumentTypeMismatchException e) { |
| 92 | + assert e.getRequiredType() != null; |
91 | 93 | return ResponseEntity.badRequest()
|
92 |
| - .body(ResultMsg.fail("参数类型不匹配,参数" + e.getPropertyName() + "类型应该为" + e.getRequiredType())); |
| 94 | + .body(ResultMsg.fail("参数类型不匹配,参数" + e.getName() + "类型应该为" + e.getRequiredType().getSimpleName())); |
93 | 95 | }
|
94 | 96 |
|
95 | 97 | @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
|
|
0 commit comments