API接口文档生成工具:Swagger2这个东西,我相信大家或多或少的都有所了解。今天我们通过SpringBoot来集成它然后构建API文档。首先我们要搭建出来一个SpringBoot的环境 然后才可以进行下面的操作。
老规矩 ,先写一个Hello Word测试下环境。
启动项目,浏览器地址栏输入http://localhost:8080/hello
接着集成swagger并对它进行一些基本的配置
老规矩先导包
因为springboot我使用的是最新的 所以它的包我也使用的是最新的 它的坐标可以在maven仓库中查看
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
写个config
package cn.qisui.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
@Bean
//配置swagger的Docket的bean实例
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// .enable(false)//是否启用swagger 如果为false 则不能在浏览器中访问
.select()
//配置要扫描接口的方式
.apis(RequestHandlerSelectors.basePackage("cn.qisui.swagger.controller"))
//过滤什么路径
// .paths(PathSelectors.ant("/qisui/**"))
.build();
}
//配置swagger信息
private ApiInfo apiInfo(){
//作者信息 这个东西是可以自定义的
Contact contact = new Contact("丁七岁", "https://me.csdn.net/qq_43612538", "1830944692@qq.com");
return new ApiInfo("丁七岁的SwaggerAPI文档",
"长风破浪会有时,直挂云帆济沧海!",
"V1.0",
"https://me.csdn.net/qq_43612538",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
apis(RequestHandlerSelectors.basePackage(“cn.qisui.swagger.controller”))
这个是要扫描的包
.paths(PathSelectors.ant("/qisui/**"))
这个是过滤掉什么
但是如果你什么也不想弄 也是可以的它有默认的设置
来看下结果。。。