|
1 | 1 | package top.cadecode.framework.handler; |
2 | 2 |
|
3 | 3 | import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.springframework.beans.TypeMismatchException; |
4 | 5 | import org.springframework.boot.web.error.ErrorAttributeOptions; |
5 | 6 | import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; |
6 | 7 | import org.springframework.http.converter.HttpMessageNotReadableException; |
7 | | -import org.springframework.web.bind.MissingServletRequestParameterException; |
| 8 | +import org.springframework.web.bind.ServletRequestBindingException; |
8 | 9 | import org.springframework.web.bind.annotation.ControllerAdvice; |
9 | 10 | import org.springframework.web.bind.annotation.ExceptionHandler; |
10 | 11 | import org.springframework.web.bind.annotation.ResponseBody; |
@@ -54,12 +55,17 @@ public CommonResponse<Object> handleOtherException(Exception e, HttpServletReque |
54 | 55 | /** |
55 | 56 | * 处理 Spring MVC 异常 |
56 | 57 | */ |
57 | | - @ExceptionHandler({MissingServletRequestParameterException.class, HttpMessageNotReadableException.class}) |
| 58 | + @ExceptionHandler({ |
| 59 | + ServletRequestBindingException.class, |
| 60 | + HttpMessageNotReadableException.class, |
| 61 | + TypeMismatchException.class |
| 62 | + }) |
58 | 63 | @ResponseBody |
59 | 64 | public CommonResponse<Object> handleMvcException(Exception e, HttpServletRequest request) { |
60 | 65 | log.error("Spring MVC Exception Handler =>", e); |
61 | 66 | String requestURI = request.getRequestURI(); |
62 | | - if (e instanceof MissingServletRequestParameterException) { |
| 67 | + if (e instanceof ServletRequestBindingException |
| 68 | + || e instanceof TypeMismatchException) { |
63 | 69 | return CommonResponse.of(ClientErrorEnum.REQ_PARAM_INVALID) |
64 | 70 | .errorMsg(e.getMessage()) |
65 | 71 | .path(requestURI); |
|
0 commit comments