本文主要是介绍同时使用接口文档swagger和knife4j,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目场景:
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配置
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>3.0.0</version></dependency>
package com.pie.common.swagger.config;@Configuration
@EnableSwagger2
@EnableAutoConfiguration
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
public class SwaggerAutoConfiguration
{/*** 默认的排除路径,排除Spring Boot默认的错误处理路径和端点*/private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**");private static final String BASE_PATH = "/**";@Bean@ConditionalOnMissingBeanpublic SwaggerProperties swaggerProperties(){return new SwaggerProperties();}@Beanpublic Docket api(SwaggerProperties swaggerProperties){// base-path处理if (swaggerProperties.getBasePath().isEmpty()){swaggerProperties.getBasePath().add(BASE_PATH);}// noinspection uncheckedList<Predicate<String>> basePath = new ArrayList<Predicate<String>>();swaggerProperties.getBasePath().forEach(path -> basePath.add(PathSelectors.ant(path)));// exclude-path处理if (swaggerProperties.getExcludePath().isEmpty()){swaggerProperties.getExcludePath().addAll(DEFAULT_EXCLUDE_PATH);}List<Predicate<String>> 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 List<SecurityScheme> securitySchemes(){List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));return apiKeyList;}/*** 安全上下文*/private List<SecurityContext> securityContexts(){List<SecurityContext> securityContexts = new ArrayList<>();securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).operationSelector(o -> o.requestMappingPattern().matches("/.*")).build());return securityContexts;}/*** 默认的全局鉴权策略** @return*/private List<SecurityReference> defaultAuth(){AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];authorizationScopes[0] = authorizationScope;List<SecurityReference> 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--><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.2</version></dependency>
在查询接口以上两个字段deptName
和orderNum
会被识别成必填字段。
原因分析:
应该是swagger对注解@NotBlank(message = "部门名称不能为空")
没有生效。
解决方案:
目前还不知道为什么swagger没有对注解生效,也没有验证这个必填问题是否对前端接口调用有影响,期待大佬的解疑!!!
这篇关于同时使用接口文档swagger和knife4j的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!