|
|
@@ -19,6 +19,8 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
+import org.springframework.context.MessageSourceResolvable;
|
|
|
+import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
@@ -27,15 +29,19 @@ import org.springframework.validation.BindException;
|
|
|
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
|
|
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.method.annotation.HandlerMethodValidationException;
|
|
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
|
|
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
|
|
+import org.xnio.Result;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* Web 全局异常处理自动配置类
|
|
|
@@ -165,6 +171,34 @@ public class GlobalWebExceptionHandlerAutoConfiguration {
|
|
|
return createResponseEntity(HttpStatus.OK, ret);
|
|
|
}
|
|
|
|
|
|
+ // 新增:把参数缺失异常,统一成和你业务异常一模一样的返回格式!
|
|
|
+ @ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
+ public ResponseEntity<ApiResult<Void>> handleMissingParamException(MissingServletRequestParameterException ex) {
|
|
|
+ String paramName = ex.getParameterName();
|
|
|
+ // 1. 汉化提示语
|
|
|
+ String errorMsg = String.format("请求失败,缺少必需的参数: [%s]", paramName);
|
|
|
+ GlobalWebExceptionI18nMessageEnum msgEnum = GlobalWebExceptionI18nMessageEnum.GLOBAL__UNACCEPTABLE_PARAMETERS;
|
|
|
+ msgEnum.setDefaultValue(errorMsg);
|
|
|
+ ApiResult<Void> ret = ApiResult.fail(HttpStatus.NOT_ACCEPTABLE.value(), this.determineI18nMessage(msgEnum));
|
|
|
+ return createResponseEntity(HttpStatus.OK, ret);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验格式
|
|
|
+ @ExceptionHandler(HandlerMethodValidationException.class)
|
|
|
+ public ResponseEntity<ApiResult<Void>> handleMethodValidationException(HandlerMethodValidationException ex) {
|
|
|
+ // 1. 汉化提示语
|
|
|
+ String errorMsg = ex.getParameterValidationResults().stream()
|
|
|
+ .flatMap(result -> result.getResolvableErrors().stream())
|
|
|
+ .map(MessageSourceResolvable::getDefaultMessage)
|
|
|
+ .filter(Objects::nonNull) // 加这一行:过滤掉所有为 null 的提示语
|
|
|
+ .findFirst()
|
|
|
+ .orElse("参数格式不正确");
|
|
|
+ GlobalWebExceptionI18nMessageEnum msgEnum = GlobalWebExceptionI18nMessageEnum.GLOBAL__UNACCEPTABLE_PARAMETERS;
|
|
|
+ msgEnum.setDefaultValue(errorMsg);
|
|
|
+ ApiResult<Void> ret = ApiResult.fail(HttpStatus.NOT_ACCEPTABLE.value(), this.determineI18nMessage(msgEnum));
|
|
|
+ return createResponseEntity(HttpStatus.OK, ret);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* JSR303 表单参数校验失败,或入参格式转换失败
|
|
|
* 需在 Controller 层使用@Valid注解
|
|
|
@@ -189,7 +223,6 @@ public class GlobalWebExceptionHandlerAutoConfiguration {
|
|
|
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeNotSupportedException.class})
|
|
|
public ResponseEntity<ApiResult<Void>> handleServletException(ServletException e, HttpServletRequest request) {
|
|
|
this.logError(e, request);
|
|
|
-
|
|
|
GlobalWebExceptionI18nMessageEnum msgEnum = GlobalWebExceptionI18nMessageEnum.GLOBAL__METHOD_NOT_ALLOWED;
|
|
|
ApiResult<Void> ret = ApiResult.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), this.determineI18nMessage(msgEnum));
|
|
|
return createResponseEntity(HttpStatus.OK, ret);
|