SpringBoot整合Swagger2
一、Swagger2简介
Swagger是一个功能强大的在线API文档的框架,其提供了在线文档的查阅和测试功能。目前它的版本为2.x,继而称为Swagger2.
利用其很容易构建RESTful风格的API,在SpringBoot中集成Swagger2,有以下五个步骤:
1、引入依赖--【springfox-swagger2和springfox-swagger-ui】
在工程的pom文件中引入依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
2、配置Swagger2 --【写配置类Swagger2】
写一个配置类Swagger2 ,在类的上方加上@Configuration 注解, 表明是一个配置类,加上@EnableSwagger2 开启Swagger2 的功能。在配置类Swagger2 中需要注入一个Docket的Bean,该Bean包含了apilnfo--基本API文档的描述信息,以及包扫描的基本包名等信息,代码如下:com/example/config/Swagger2.java
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.