花乡做网站公司,合肥建工学校,小程序制作推广费用,wordpress网站数据迁移项目场景#xff1a;
springboot项目中同时使用接口文档swagger和knife4j 问题描述
在实体类中设置了字段必填的属性#xff0c;在访问接口文档时出现异常
实体类关键代码片段
/*** 部门表 sys_dept*/
public class SysDept extends BaseEntity
{private static final lo…项目场景
springboot项目中同时使用接口文档swagger和knife4j 问题描述
在实体类中设置了字段必填的属性在访问接口文档时出现异常
实体类关键代码片段
/*** 部门表 sys_dept*/
public class SysDept extends BaseEntity
{private static final long serialVersionUID 1L;/** 部门ID */private Long deptId;/** 部门名称 */private String deptName;/** 显示顺序 */private Integer orderNum;NotBlank(message 部门名称不能为空)Size(min 0, max 30, message 部门名称长度不能超过30个字符)public String getDeptName(){return deptName;}public void setDeptName(String deptName){this.deptName deptName;}NotNull(message 显示顺序不能为空)public Integer getOrderNum(){return orderNum;}public void setOrderNum(Integer orderNum){this.orderNum orderNum;}
}异常描述
1没有加入knife4j配置仅使用swagger配置 dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger2/artifactIdversion3.0.0/version/dependencypackage com.pie.common.swagger.config;Configuration
EnableSwagger2
EnableAutoConfiguration
ConditionalOnProperty(name swagger.enabled, matchIfMissing true)
public class SwaggerAutoConfiguration
{/*** 默认的排除路径排除Spring Boot默认的错误处理路径和端点*/private static final ListString DEFAULT_EXCLUDE_PATH Arrays.asList(/error, /actuator/**);private static final String BASE_PATH /**;BeanConditionalOnMissingBeanpublic SwaggerProperties swaggerProperties(){return new SwaggerProperties();}Beanpublic Docket api(SwaggerProperties swaggerProperties){// base-path处理if (swaggerProperties.getBasePath().isEmpty()){swaggerProperties.getBasePath().add(BASE_PATH);}// noinspection uncheckedListPredicateString basePath new ArrayListPredicateString();swaggerProperties.getBasePath().forEach(path - basePath.add(PathSelectors.ant(path)));// exclude-path处理if (swaggerProperties.getExcludePath().isEmpty()){swaggerProperties.getExcludePath().addAll(DEFAULT_EXCLUDE_PATH);}ListPredicateString excludePath new ArrayList();swaggerProperties.getExcludePath().forEach(path - excludePath.add(PathSelectors.ant(path)));ApiSelectorBuilder builder new Docket(DocumentationType.SWAGGER_2).host(swaggerProperties.getHost()).apiInfo(apiInfo(swaggerProperties)).select().apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()));swaggerProperties.getBasePath().forEach(p - builder.paths(PathSelectors.ant(p)));swaggerProperties.getExcludePath().forEach(p - builder.paths(PathSelectors.ant(p).negate()));return builder.build().securitySchemes(securitySchemes()).securityContexts(securityContexts()).pathMapping(/);}/*** 安全模式这里指定token通过Authorization头请求头传递*/private ListSecurityScheme securitySchemes(){ListSecurityScheme apiKeyList new ArrayListSecurityScheme();apiKeyList.add(new ApiKey(Authorization, Authorization, header));return apiKeyList;}/*** 安全上下文*/private ListSecurityContext securityContexts(){ListSecurityContext securityContexts new ArrayList();securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).operationSelector(o - o.requestMappingPattern().matches(/.*)).build());return securityContexts;}/*** 默认的全局鉴权策略** return*/private ListSecurityReference defaultAuth(){AuthorizationScope authorizationScope new AuthorizationScope(global, accessEverything);AuthorizationScope[] authorizationScopes new AuthorizationScope[1];authorizationScopes[0] authorizationScope;ListSecurityReference securityReferences new ArrayList();securityReferences.add(new SecurityReference(Authorization, authorizationScopes));return securityReferences;}private ApiInfo apiInfo(SwaggerProperties swaggerProperties){return new ApiInfoBuilder().title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription()).license(swaggerProperties.getLicense()).licenseUrl(swaggerProperties.getLicenseUrl()).termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl()).contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail())).version(swaggerProperties.getVersion()).build();}
} 在查询接口以上两个字段deptName和orderNum不会被识别成必填字段
2在以上基础加入knife4j依赖
!-- knife4j--dependencygroupIdcom.github.xiaoymin/groupIdartifactIdknife4j-spring-boot-starter/artifactIdversion3.0.2/version/dependency在查询接口以上两个字段deptName和orderNum会被识别成必填字段。 原因分析
应该是swagger对注解NotBlank(message 部门名称不能为空)没有生效。 解决方案 目前还不知道为什么swagger没有对注解生效也没有验证这个必填问题是否对前端接口调用有影响期待大佬的解疑