1212import org .springframework .http .converter .HttpMessageNotReadableException ;
1313import org .springframework .util .StringUtils ;
1414import org .springframework .validation .BindException ;
15- import org .springframework .validation .BindingResult ;
1615import org .springframework .validation .FieldError ;
17- import org .springframework .web .bind .MethodArgumentNotValidException ;
1816import org .springframework .web .bind .MissingServletRequestParameterException ;
1917import org .springframework .web .bind .annotation .ExceptionHandler ;
2018import org .springframework .web .bind .annotation .RestControllerAdvice ;
@@ -78,19 +76,18 @@ public UnifyResponseVO<Map<String, Object>> processException(BindException excep
7876 HttpServletRequest request ,
7977 HttpServletResponse response ) {
8078 log .error (exception .toString ());
81- return getMapUnifyResponseVOByErrorList (request , response , exception );
82- }
83-
84- /**
85- * 将请求体解析并绑定到 java bean 时,如果出错,则抛出 MethodArgumentNotValidException 异常
86- */
87- @ ExceptionHandler ({MethodArgumentNotValidException .class })
88- public UnifyResponseVO <Map <String , Object >> processException (MethodArgumentNotValidException exception ,
89- HttpServletRequest request ,
90- HttpServletResponse response ) {
91- log .error ("" , exception );
92- BindingResult bindingResult = exception .getBindingResult ();
93- return getMapUnifyResponseVOByErrorList (request , response , bindingResult );
79+ Map <String , Object > msg = new HashMap <>();
80+ exception .getAllErrors ().forEach (error -> {
81+ if (error instanceof FieldError ) {
82+ FieldError fieldError = (FieldError ) error ;
83+ msg .put (com .baomidou .mybatisplus .core .toolkit .StringUtils .camelToUnderline (fieldError .getField ()),
84+ fieldError .getDefaultMessage ());
85+ } else {
86+ msg .put (com .baomidou .mybatisplus .core .toolkit .StringUtils .camelToUnderline (error .getObjectName ()),
87+ error .getDefaultMessage ());
88+ }
89+ });
90+ return getMapUnifyResponseVO (request , response , msg );
9491 }
9592
9693 /**
@@ -100,7 +97,7 @@ public UnifyResponseVO<Map<String, Object>> processException(MethodArgumentNotVa
10097 public UnifyResponseVO <Map <String , Object >> processException (ConstraintViolationException exception ,
10198 HttpServletRequest request ,
10299 HttpServletResponse response ) {
103- log .error (exception . toString () );
100+ log .error ("" , exception );
104101 Map <String , Object > msg = new HashMap <>();
105102 exception .getConstraintViolations ().forEach (constraintViolation -> {
106103 String template = constraintViolation .getMessage ();
@@ -281,27 +278,10 @@ private UnifyResponseVO<Map<String, Object>> getMapUnifyResponseVO(HttpServletRe
281278 return unifyResponse ;
282279 }
283280
284- private UnifyResponseVO <Map <String , Object >> getMapUnifyResponseVOByErrorList (HttpServletRequest request ,
285- HttpServletResponse response ,
286- BindingResult bindingResult ) {
287- Map <String , Object > msg = new HashMap <>();
288- bindingResult .getAllErrors ().forEach (error -> {
289- if (error instanceof FieldError ) {
290- FieldError fieldError = (FieldError ) error ;
291- msg .put (com .baomidou .mybatisplus .core .toolkit .StringUtils .camelToUnderline (fieldError .getField ()),
292- fieldError .getDefaultMessage ());
293- } else {
294- msg .put (com .baomidou .mybatisplus .core .toolkit .StringUtils .camelToUnderline (error .getObjectName ()),
295- error .getDefaultMessage ());
296- }
297- });
298- return getMapUnifyResponseVO (request , response , msg );
299- }
300-
301281 /**
302282 * 传参类型错误时,用于消息转换
303283 *
304- * @param throwable
284+ * @param throwable 异常
305285 * @return 错误信息
306286 */
307287 private String convertMessage (Throwable throwable ) {
0 commit comments