Swagger 3.0 配置梳理
swagger 3.0 于2020.07月发布,其实3.0版本与2.x版本大同小异。主要步骤:添加依赖开启swagger配置摘要信息访问 Swagger 页面添加依赖<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId&
swagger 3.0 于2020.07月发布,其实3.0版本与2.x版本大同小异。
主要步骤:
- 添加依赖
- 开启swagger (启动类 / 配置文件 )
- 配置摘要信息
- 访问 Swagger 页面
一、添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${springfox.version}</version>
</dependency>
二、开启swagger
1) 启动类的启动方式
在springboot 中可以不在启动类上加上这个配置注解,这种方式不灵活,所以我更喜欢在配置文件上进行开启
@EnableOpenApi //开启 Swagger
@SpringBootApplication
public class Application {
}
启动类上的启动方式,自动配置的具体理解
@Configuration
@EnableConfigurationProperties(SpringfoxConfigurationProperties.class)
@ConditionalOnProperty(value = "springfox.documentation.enabled", havingValue = "true", matchIfMissing = true)
@Import({
OpenApiDocumentationConfiguration.class,
SpringDataRestConfiguration.class,
BeanValidatorPluginsConfiguration.class,
Swagger2DocumentationConfiguration.class,
SwaggerUiWebFluxConfiguration.class,
SwaggerUiWebMvcConfiguration.class
})
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, RepositoryRestMvcAutoConfiguration.class })
public class OpenApiAutoConfiguration {
}
先查看 springboot 的 spring.factories 自动化配置文件中就能看到
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\springfox.boot.starter.autoconfigure.OpenApiAutoConfiguration
Spring Boot 的启动类或配置类中添加 @EnableOpenApi, 该注解加不加作用不明显,原因如下:
a. 根据 OpenApiAutoConfiguration 上的 @ConditionalOnProperty 条件注解中的定义,该注解中matchIfMissing=true, 指配置文件application.properties中未进行对应属性配置时的默认处理。true 没有对应的属性配置,则自动配置默认生效,反之false 不生效。
b. 如果在 application.properties 中设置配置属性 springfox.documentation.enabled=false,即关闭了 swagger 功能,此时自动化配置类就不执行。可以通过 @EnableOpenApi 注解导入 OpenApiDocumentationConfiguration 配置类。
技术上来说逻辑是这样,不过应用中暂未发现这样的需求(即在 application.properties 中关闭 swagger,再通过 @EnableOpenApi 注解开启)。
2) 配置文件的启动方式
application.yml
spring:
#运行时的配置文件
profiles:
active: swagger
#启动 swagger
springfox:
documentation:
enabled: true
三、配置摘要信息
application-swagger.yml
swagger:
version: 1.0.0
title: 文档名称
description: 文档的整体描述
serverurl: http://localhost:8080
contact:
name: 联系人
url: 联系网页
email: 联系邮箱
apis:
#请求组(a)
- id: 请求组ID
url: 映射扫描路径 ,例如 com.*.controller
name: api组名
SwaggerYml 配置文件的属性
@Data
@Component
@ConfigurationProperties(prefix = "swagger")
public class SwaggerYml {
private String version;
private String title;
private String description;
private String serverUrl;
private Contact contact;
private List<RestApi> apis;
@Data
public static class Contact{
private String url;
private String email;
private String name;
}
@Data
public static class RestApi{
private String id;
private String url;
private String name;
}
public SwaggerYml getInstance(){
return this;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
swagger配置项
@Configuration
public class SwaggerConfig {
private Map<String,SwaggerYmlVo.RestApi> restApiMap = new HashMap<>();
@Autowired
private SwaggerYml swaggerYml;
public SwaggerConfig(SwaggerYml swaggerYml) {
this.swaggerYml = swaggerYml;
List<SwaggerYml.RestApi> apis = swaggerYml.getApis();
if (apis.size() > 0){
restApiMap = apis.stream().collect(Collectors.toMap(SwaggerYml.RestApi::getId, Function.identity(), (a, b) -> a));
}
}
@Bean
public Docket restA() {
SwaggerYml.RestApi restApi = restApiMap.get(请求组ID);
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(restApi.getUrl())) // 设置扫描路径
.paths(PathSelectors.any())
.build()
.groupName(restApi.getName())
;
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
*
* @return
*/
private ApiInfo apiInfo() {
SwaggerYml.Contact ymlVoContact = swaggerYml.getContact();
Contact contact = new Contact(ymlVoContact.getName(), ymlVoContact.getUrl(), ymlVoContact.getEmail());
return new ApiInfoBuilder()
.title(swaggerYml.getTitle())
.description(swaggerYml.getDescription())
.version(swaggerYml.getVersion())
.termsOfServiceUrl(swaggerYml.getServerUrl())
.contact(contact)
.build();
}
}
访问 Swagger 页面
新版本的 Swagger 访问地址和老版本的地址是不同的,新版的访问地址是localhost:8080/swagger-ui/,文档:localhost:8080/v3/api-docs
仅供学习。
更多推荐
所有评论(0)