Swagger接口文档使用(三种)
1.使用直接使用springdoc-OpenAPI3:
官网地址
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-ui</artifactId><version>1.7.0</version>
</dependency>
application.yml中添加SpringDoc配置 (根据需要配置即可)
springdoc:swagger-ui:path: /swagger.html #自定义文档访问路径,默认/swagger-ui/index.htmlapi-docs:path: /api-docs #自定义文档api元数据访问路径,默认访问路径是/v3/api-docsgroup-configs: #接口分组配置- group: 1.APP端业务接口packages-to-scan: com.jungle.app #包路径- group: 2.管理端接口packages-to-scan: com.jungle.admin
更多yml配置可查看官网地址
http://localhost:8080/swagger.html
在application.yml配置文件中添加自定义配置信息:
# 自定义Swagger配置
swagger:# 是否开启swaggerenabled: true#路径访问前缀contextPath:info:# 标题title: 'jungle——接口文档'# 描述description: '这是描述'# 版本version: '版本号: 2.0.1'# 作者信息contact:name: Jungleemail: example@qq.com
SwaggerProperties配置属性类,读取swagger自定义配置信息
/*** swagger 配置属性** @author Lion Li*/
@Data
@Component
@ConfigurationProperties(prefix = "swagger")
public class SwaggerProperties {/*** 是否开启 openApi 文档*/private Boolean enabled = true;private String contextPath;@NestedConfigurationPropertyprivate InfoProperties info = new InfoProperties();@NestedConfigurationPropertyprivate ExternalDocumentation externalDocs;private List<Tag> tags = null;@NestedConfigurationPropertyprivate Paths paths = null;@NestedConfigurationPropertyprivate Components components = null;/*** <p>* 文档的基础属性信息* </p>** 为了 springboot 自动生产配置提示信息,所以这里复制一个类出来*/@Datapublic static class InfoProperties {private String title = "";private String description = null;@NestedConfigurationPropertyprivate Contact contact = null;@NestedConfigurationPropertyprivate License license = null;private String version = null;}
//注入SwaggerProperties@Autowiredprivate SwaggerProperties swaggerProperties;//配置类中装配OpenAPI对象,配置文档信息@Bean@ConditionalOnMissingBean(OpenAPI.class)public OpenAPI openApi() {OpenAPI openApi = new OpenAPI();// 文档基本信息SwaggerProperties.InfoProperties infoProperties = swaggerProperties.getInfo();Info info = convertInfo(infoProperties);openApi.info(info);// 扩展文档信息openApi.externalDocs(swaggerProperties.getExternalDocs());openApi.tags(swaggerProperties.getTags());openApi.paths(swaggerProperties.getPaths());openApi.components(swaggerProperties.getComponents());List<SecurityRequirement> list = new ArrayList<>();list.add(new SecurityRequirement().addList("apikey"));openApi.security(list);return openApi;}
配置swagger接口备注来源于Javadoc
pom文件中引入以下依赖:
<!--在运行时读取 Javadoc 注释-->
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-javadoc</artifactId><version>1.6.12</version><!--包含依赖 :therapi-runtime-javadoc-->
</dependency>
在pom文件的 build > plugins > plugin > configuration标签中添加:
<annotationProcessorPaths><path><!-- 注释处理器 --><groupId>com.github.therapi</groupId><artifactId>therapi-runtime-javadoc-scribe</artifactId><version>0.15.0</version></path>
</annotationProcessorPaths>
如果同时存在 swagger注解说明 和 Javadoc 注释。将使用swagger注解说明的值。
springdoc文档地址:https://springdoc.org/#getting-started
2.使用knife4j升级版本v4.x
官方文档:https://doc.xiaominfo.com/docs/quick-start
(1)使用knife4j整合OpenAPI2.0(springboot2.x)
pom文件中引入依赖
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi2-spring-boot-starter</artifactId><version>4.3.0</version>
</dependency>
application.yml中添加配置信息(根据需要配置即可)
#knife4j openapi2.0
knife4j:enable: trueopenapi:title: Char智旅description: 'Char智旅——接口文档'email: 3224431318@qq.com concat: Jungle #作者url: https://docs.xiaominfo.comversion: v2.0.1license: Apache 2.0license-url: https://stackoverflow.com/terms-of-service-url: https://stackoverflow.com/group: #配置接口分组信息one:group-name: 1.APP端业务接口api-rule: packageapi-rule-resources:- com.jungle.apptwo:group-name: 2.系统接口api-rule: packageapi-rule-resources:- com.jungle.admin
添加WebMvcConfiguration配置类
@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {/*** 设置静态资源映射** @param registry*/public void addResourceHandlers(ResourceHandlerRegistry registry) {log.info("设置静态资源映射");registry.addResourceHandler("/doc.html").addResourceLocations("classpath:META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:META-INF/resources/webjars/");}
}
浏览器访问:http://localhost:8080/doc.html (根据实际情况)
(2)使用knife4j整合OpenAPI3.0(springboot2.x)
pom文件引入依赖
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-spring-boot-starter</artifactId><version>4.3.0</version>
</dependency>
application.yml配置文件内容可完全参考springdoc-openapi的项目说明,Knife4j只提供了增强部分,如果要启用Knife4j的增强功能,可以在配置文件中进行开启,,,knife4j的增强配置,不需要增强可以不配,增强功能可通过官方文档查看
knife4j:enable: truesetting:language: zh_cnwriter-with-default-pretty-printer: true
浏览器访问:
http://localhost:8080/swagger.html
)