mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-14 13:42:07 +08:00
1、移除swagger固定header:organId;2、全局异常处理新增HttpMessageNotReadableException捕获;
This commit is contained in:
-15
@@ -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.Contact;
|
||||||
import io.swagger.v3.oas.models.info.Info;
|
import io.swagger.v3.oas.models.info.Info;
|
||||||
import io.swagger.v3.oas.models.info.License;
|
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.media.StringSchema;
|
||||||
import io.swagger.v3.oas.models.parameters.Parameter;
|
import io.swagger.v3.oas.models.parameters.Parameter;
|
||||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||||
@@ -118,24 +117,10 @@ public class ChenfengSwaggerAutoConfiguration {
|
|||||||
.group(group)
|
.group(group)
|
||||||
.pathsToMatch("/admin-api/" + path + "/**", "/app-api/" + path + "/**")
|
.pathsToMatch("/admin-api/" + path + "/**", "/app-api/" + path + "/**")
|
||||||
.addOperationCustomizer((operation, handlerMethod) -> operation
|
.addOperationCustomizer((operation, handlerMethod) -> operation
|
||||||
.addParametersItem(buildTenantHeaderParameter())
|
|
||||||
.addParametersItem(buildSecurityHeaderParameter()))
|
.addParametersItem(buildSecurityHeaderParameter()))
|
||||||
.build();
|
.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 认证请求头参数
|
* 构建 Authorization 认证请求头参数
|
||||||
*
|
*
|
||||||
|
|||||||
+14
-4
@@ -15,6 +15,7 @@ import com.cf.imes.framework.common.util.servlet.ServletUtils;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.dao.DuplicateKeyException;
|
import org.springframework.dao.DuplicateKeyException;
|
||||||
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
import org.springframework.security.access.AccessDeniedException;
|
import org.springframework.security.access.AccessDeniedException;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.validation.BindException;
|
import org.springframework.validation.BindException;
|
||||||
@@ -88,9 +89,11 @@ public class GlobalExceptionHandler {
|
|||||||
return serviceExceptionHandler((ServiceException) ex);
|
return serviceExceptionHandler((ServiceException) ex);
|
||||||
}
|
}
|
||||||
if (ex instanceof AccessDeniedException) {
|
if (ex instanceof AccessDeniedException) {
|
||||||
ex.printStackTrace();
|
|
||||||
return accessDeniedExceptionHandler(request, (AccessDeniedException) ex);
|
return accessDeniedExceptionHandler(request, (AccessDeniedException) ex);
|
||||||
}
|
}
|
||||||
|
if (ex instanceof HttpMessageNotReadableException) {
|
||||||
|
return httpMessageNotReadableExceptionHandler(request, (HttpMessageNotReadableException) ex);
|
||||||
|
}
|
||||||
return defaultExceptionHandler(request, ex);
|
return defaultExceptionHandler(request, ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +116,7 @@ public class GlobalExceptionHandler {
|
|||||||
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
||||||
public CommonResult methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) {
|
public CommonResult methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) {
|
||||||
log.warn("[missingServletRequestParameterExceptionHandler]", 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();
|
FieldError fieldError = ex.getFieldError();
|
||||||
assert fieldError != null; // 断言,避免告警
|
assert fieldError != null; // 断言,避免告警
|
||||||
String defaultMessage = fieldError.getDefaultMessage();
|
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));
|
return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", defaultMessage));
|
||||||
} else {
|
} 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);
|
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
|
* 处理业务异常 ServiceException
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user