0%

为WebFlux配置Swagger

一、Swagger是什么

一般做后端,都需要向前端提供API,但是因为后端一般都赖得写API文档,所以就出现了个这东西,用于生成在线的API文档,同时提供了在线测试API的用户接口。

二、配置

一般在SpringMVC都有有相关的blog去记录配置了,但是一般都没有WebFlux相关的配置文档,这里配一个:

Maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-webflux</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-integration</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-integration-webflux</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>


<repositories>
<repository>
<id>spring-libs-milestone</id>
<name>Spring Milestone Maven Repository</name>
<url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
</repository>
</repositories>

application.yaml

1
2
3
4
spring:
resources:
static-locations:
- classpath:/META-INF/resources/

Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux;

/**
* Swagger configuration
*
* @author kut
*/
@Configuration
@EnableSwagger2WebFlux
@Slf4j
public class SwaggerConfiguration {

@Bean
public Docket buildPlatformApiDocket() {
ApiInfo info = new ApiInfoBuilder()
.title("AM Platform API")
.description("AM Platform API")
.version("1.0")
.contact(new Contact("Kut Zhang", "https://www.sanlea.com", "kut@sanlea.cn"))
.build();

return new Docket(DocumentationType.SWAGGER_2)
.groupName("AM Platform API")
.apiInfo(info)
.select()
.apis(RequestHandlerSelectors.basePackage("am.pc.api.platform"))
.paths(PathSelectors.any())
.build();
}

@Bean
public Docket buildManagementApiDocket() {
ApiInfo info = new ApiInfoBuilder()
.title("AM Management API")
.description("AM Management API")
.version("1.0")
.contact(new Contact("Kut Zhang", "https://www.sanlea.com", "kut@sanlea.cn"))
.build();

return new Docket(DocumentationType.SWAGGER_2)
.groupName("AM Management API")
.apiInfo(info)
.select()
.apis(RequestHandlerSelectors.basePackage("am.pc.api.management"))
.paths(PathSelectors.any())
.build();
}
}

访问:

1
http://localhost:8080/swagger_ui.html

三、总结

Swagger是个好东西!