1、移除swagger固定header:organId;2、全局异常处理新增HttpMessageNotReadableException捕获;

This commit is contained in:
gaoqr
2024-10-18 11:20:01 +08:00
parent 81525de432
commit ab0ca666c3
2 changed files with 14 additions and 19 deletions
@@ -5,7 +5,6 @@ import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
@@ -118,24 +117,10 @@ public class ChenfengSwaggerAutoConfiguration {
.group(group)
.pathsToMatch("/admin-api/" + path + "/**", "/app-api/" + path + "/**")
.addOperationCustomizer((operation, handlerMethod) -> operation
.addParametersItem(buildTenantHeaderParameter())
.addParametersItem(buildSecurityHeaderParameter()))
.build();
}
/**
* 构建 Tenant 组织编号请求头参数
*
* @return 多组织参数
*/
private static Parameter buildTenantHeaderParameter() {
return new Parameter()
.name(HEADER_ORGAN_ID) // header 名
.description("组织编号") // 描述
.in(String.valueOf(SecurityScheme.In.HEADER)) // 请求 header
.schema(new IntegerSchema()._default(1L).name(HEADER_ORGAN_ID).description("组织编号")); // 默认:使用组织编号为 1
}
/**
* 构建 Authorization 认证请求头参数
*
@@ -15,6 +15,7 @@ import com.cf.imes.framework.common.util.servlet.ServletUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.util.Assert;
import org.springframework.validation.BindException;
@@ -88,9 +89,11 @@ public class GlobalExceptionHandler {
return serviceExceptionHandler((ServiceException) ex);
}
if (ex instanceof AccessDeniedException) {
ex.printStackTrace();
return accessDeniedExceptionHandler(request, (AccessDeniedException) ex);
}
if (ex instanceof HttpMessageNotReadableException) {
return httpMessageNotReadableExceptionHandler(request, (HttpMessageNotReadableException) ex);
}
return defaultExceptionHandler(request, ex);
}
@@ -113,7 +116,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public CommonResult methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) {
log.warn("[missingServletRequestParameterExceptionHandler]", ex);
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", ex.getMessage()));
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误,%s:%s", ex.getName(), ex.getValue()));
}
/**
@@ -136,10 +139,11 @@ public class GlobalExceptionHandler {
FieldError fieldError = ex.getFieldError();
assert fieldError != null; // 断言,避免告警
String defaultMessage = fieldError.getDefaultMessage();
if (CharSequenceUtil.isNotEmpty(defaultMessage)) {
// 如果是校验器来的给出校验注解的提示
if (CharSequenceUtil.isNotEmpty(defaultMessage) && fieldError.contains(ConstraintViolation.class)) {
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", defaultMessage));
} else {
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", fieldError.getRejectedValue()));
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确%s:%s", fieldError.getField(), fieldError.getRejectedValue()));
}
}
@@ -207,6 +211,12 @@ public class GlobalExceptionHandler {
return CommonResult.error(FORBIDDEN);
}
@ExceptionHandler(value = HttpMessageNotReadableException.class)
public CommonResult httpMessageNotReadableExceptionHandler(HttpServletRequest req, HttpMessageNotReadableException ex) {
log.warn("[httpMessageNotReadableExceptionHandler]", ex);
return CommonResult.error(BAD_REQUEST.getCode(), "请求参数不合法");
}
/**
* 处理业务异常 ServiceException
*