Spring Boot学习(五)之使用Swagger2构建强大的RESTful API文档

  随着前后端的分离,接口文档变的尤其重要,今天我们来说一说用SWAGGER2,来风骚的生成api文档。配置很简单,废话少说,直接上代码:

pom.xml文件

01 <parent>
02         <groupId>org.springframework.boot</groupId>
03         <artifactId>spring-boot-starter-parent</artifactId>
04         <version>1.5.8.RELEASE</version>
05         <relativePath/> <!-- lookup parent from repository -->
06     </parent>
07  
08     <properties>
09         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10         <java.version>1.8</java.version>
11     </properties>
12  
13     <dependencies>
14         <dependency>
15             <groupId>org.springframework.boot</groupId>
16             <artifactId>spring-boot-starter</artifactId>
17         </dependency>
18  
19         <dependency>
20             <groupId>org.springframework.boot</groupId>
21             <artifactId>spring-boot-starter-test</artifactId>
22             <scope>test</scope>
23         </dependency>
24  
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-web</artifactId>
28         </dependency>
29  
30         <dependency>
31             <groupId>io.springfox</groupId>
32             <artifactId>springfox-swagger2</artifactId>
33             <version>2.2.2</version>
34         </dependency>
35         <dependency>
36             <groupId>io.springfox</groupId>
37             <artifactId>springfox-swagger-ui</artifactId>
38             <version>2.2.2</version>
39         </dependency>
40  
41     </dependencies>
42      
43     <build>
44         <plugins>
45             <plugin>
46                 <groupId>org.springframework.boot</groupId>
47                 <artifactId>spring-boot-maven-plugin</artifactId>
48             </plugin>
49         </plugins>
50     </build>

首先,我们需要一个Spring Boot实现的RESTful API工程,若您没有做过这类内容,建议先阅读
Spring Boot构建一个较为复杂的RESTful APIs和单元测试

创建Swagger2配置类

在Application.java启动类的同级创建Swagger2的配置类Swagger2。

01 @Configuration
02 @EnableSwagger2
03 public class Swagger2 {
04  
05     @Bean
06     public Docket createRestApi() {
07         return new Docket(DocumentationType.SWAGGER_2)
08                 .apiInfo(apiInfo())
09                 .select()
10                 .apis(RequestHandlerSelectors.basePackage("com.xiaojingg.web"))
11                 .paths(PathSelectors.any())
12                 .build();
13     }
14  
15     private ApiInfo apiInfo() {
16         return new ApiInfoBuilder()
17                 .title("Spring Boot中使用Swagger2构建RESTful APIs")
18                 .description("更多Spring Boot相关文章请关注:http://www.zuidaima.com/user/2068438360459264/blog.htm")
19                 .termsOfServiceUrl("http://www.zuidaima.com/user/2068438360459264/blog.htm")
20                 .contact("筱进GG")
21                 .version("1.0")
22                 .build();
23     }
24  
25 }

如上代码所示,通过@Configuration注解,让Spring来加载该类配置。再通过@EnableSwagger2注解来启用Swagger2。

再通过createRestApi函数创建Docket的Bean之后,apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)。select()函数返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现,本例采用指定扫描的包路径来定义,Swagger会扫描该包下所有Controller定义的API,并产生文档内容(除了被@ApiIgnore指定的请求)。

添加文档内容

在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容。如下所示,我们通过@ApiOperation注解来给API增加说明、通过@ApiImplicitParams、@ApiImplicitParam注解来给参数增加说明。

添加上之前Spring Boot学习(三)之构建RESTful API与单元测试 后;

启动启动类;

访问:http://localhost:8080/swagger-ui.html
Spring Boot学习(五)之使用Swagger2构建强大的RESTful API文档

就能看到前文所展示的RESTful API的页面。我们可以再点开具体的API请求,以POST类型的/users请求为例,可找到上述代码中我们配置的Notes信息以及参数user的描述信息,如下图所示。

Spring Boot学习(五)之使用Swagger2构建强大的RESTful API文档

配置完成了, 小伙伴们试试吧!

 欢迎大家一起交流学习SpringBoot,java等领域的技术,交流群 : 587674051 博客的源码也在里面