bugfix-调整GlobalExceptionHandler,增加InvalidFormatException处理对应RequestBody实体中 xx 属性类型为 Integer,结果传递 xx 参数类型为 String
							parent
							
								
									7a86121585
								
							
						
					
					
						commit
						35f5ee4677
					
				|  | @ -15,12 +15,14 @@ import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | |||
| import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | ||||
| import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | ||||
| import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; | ||||
| import com.fasterxml.jackson.databind.exc.InvalidFormatException; | ||||
| import jakarta.servlet.http.HttpServletRequest; | ||||
| import jakarta.validation.ConstraintViolation; | ||||
| import jakarta.validation.ConstraintViolationException; | ||||
| import jakarta.validation.ValidationException; | ||||
| import lombok.AllArgsConstructor; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.springframework.http.converter.HttpMessageNotReadableException; | ||||
| import org.springframework.security.access.AccessDeniedException; | ||||
| import org.springframework.util.Assert; | ||||
| import org.springframework.validation.BindException; | ||||
|  | @ -38,7 +40,12 @@ import java.time.LocalDateTime; | |||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
| 
 | ||||
| import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*; | ||||
| import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST; | ||||
| import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.FORBIDDEN; | ||||
| import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; | ||||
| import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.METHOD_NOT_ALLOWED; | ||||
| import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_FOUND; | ||||
| import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED; | ||||
| 
 | ||||
| /** | ||||
|  * 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号 | ||||
|  | @ -123,7 +130,7 @@ public class GlobalExceptionHandler { | |||
|      */ | ||||
|     @ExceptionHandler(MethodArgumentTypeMismatchException.class) | ||||
|     public CommonResult<?> methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) { | ||||
|         log.warn("[missingServletRequestParameterExceptionHandler]", ex); | ||||
|         log.warn("[methodArgumentTypeMismatchExceptionHandler]", ex); | ||||
|         return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", ex.getMessage())); | ||||
|     } | ||||
| 
 | ||||
|  | @ -149,6 +156,22 @@ public class GlobalExceptionHandler { | |||
|         return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", fieldError.getDefaultMessage())); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 处理 SpringMVC 请求参数类型错误 | ||||
|      * | ||||
|      * 例如说,接口上设置了 @RequestBody实体中 xx 属性类型为 Integer,结果传递 xx 参数类型为 String | ||||
|      */ | ||||
|     @ExceptionHandler(HttpMessageNotReadableException.class) | ||||
|     public CommonResult<?> methodArgumentTypeInvalidFormatExceptionHandler(HttpMessageNotReadableException ex) { | ||||
|         log.warn("[methodArgumentTypeInvalidFormatExceptionHandler]", ex); | ||||
|         if(ex.getCause() instanceof InvalidFormatException) { | ||||
|             InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause(); | ||||
|             return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue())); | ||||
|         }else { | ||||
|             return defaultExceptionHandler(ServletUtils.getRequest(), ex); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 处理 Validator 校验不通过产生的异常 | ||||
|      */ | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 yinyilong
						yinyilong