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.html
api-docs:
path: /api-docs #自定义文档api元数据访问路径,默认访问路径是/v3/api-docs
group-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:
# 是否开启swagger
enabled: true
#路径访问前缀
contextPath:
info:
# 标题
title: 'jungle——接口文档'
# 描述
description: '这是描述'
# 版本
version: '版本号: 2.0.1'
# 作者信息
contact:
name: Jungle
email: [email protected]
SwaggerProperties配置属性类,读取swagger自定义配置信息
/**
* swagger 配置属性
*
* @author Lion Li
*/
@Data
@Component
@ConfigurationProperties(prefix = "swagger")
public class SwaggerProperties {
/**
* 是否开启 openApi 文档
*/
private Boolean enabled = true;
private String contextPath;
@NestedConfigurationProperty
private InfoProperties info = new InfoProperties();
@NestedConfigurationProperty
private ExternalDocumentation externalDocs;
private List<Tag> tags = null;
@NestedConfigurationProperty
private Paths paths = null;
@NestedConfigurationProperty
private Components components = null;
/**
* <p>
* 文档的基础属性信息
* </p>
*
* 为了 springboot 自动生产配置提示信息,所以这里复制一个类出来
*/
@Data
public static class InfoProperties {
private String title = "";
private String description = null;
@NestedConfigurationProperty
private Contact contact = null;
@NestedConfigurationProperty
private License license = null;
private String version = null;
}
//注入SwaggerProperties
@Autowired
private 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: true
openapi:
title: Char智旅
description: 'Char智旅——接口文档'
email: [email protected]
concat: Jungle #作者
url: https://docs.xiaominfo.com
version: v2.0.1
license: Apache 2.0
license-url: https://stackoverflow.com/
terms-of-service-url: https://stackoverflow.com/
group: #配置接口分组信息
one:
group-name: 1.APP端业务接口
api-rule: package
api-rule-resources:
- com.jungle.app
two:
group-name: 2.系统接口
api-rule: package
api-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: true
setting:
language: zh_cn
writer-with-default-pretty-printer: true
浏览器访问:
http://localhost:8080/swagger.html
)