Skip to content

Commit ab1bac9

Browse files
committed
feat: 添加对 TypeMismatchException 的统一处理
1 parent 322c803 commit ab1bac9

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

simple-framework/src/main/java/top/cadecode/framework/handler/GlobalExceptionHandler.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package top.cadecode.framework.handler;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.TypeMismatchException;
45
import org.springframework.boot.web.error.ErrorAttributeOptions;
56
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
67
import org.springframework.http.converter.HttpMessageNotReadableException;
7-
import org.springframework.web.bind.MissingServletRequestParameterException;
8+
import org.springframework.web.bind.ServletRequestBindingException;
89
import org.springframework.web.bind.annotation.ControllerAdvice;
910
import org.springframework.web.bind.annotation.ExceptionHandler;
1011
import org.springframework.web.bind.annotation.ResponseBody;
@@ -54,12 +55,17 @@ public CommonResponse<Object> handleOtherException(Exception e, HttpServletReque
5455
/**
5556
* 处理 Spring MVC 异常
5657
*/
57-
@ExceptionHandler({MissingServletRequestParameterException.class, HttpMessageNotReadableException.class})
58+
@ExceptionHandler({
59+
ServletRequestBindingException.class,
60+
HttpMessageNotReadableException.class,
61+
TypeMismatchException.class
62+
})
5863
@ResponseBody
5964
public CommonResponse<Object> handleMvcException(Exception e, HttpServletRequest request) {
6065
log.error("Spring MVC Exception Handler =>", e);
6166
String requestURI = request.getRequestURI();
62-
if (e instanceof MissingServletRequestParameterException) {
67+
if (e instanceof ServletRequestBindingException
68+
|| e instanceof TypeMismatchException) {
6369
return CommonResponse.of(ClientErrorEnum.REQ_PARAM_INVALID)
6470
.errorMsg(e.getMessage())
6571
.path(requestURI);

0 commit comments

Comments
 (0)