springboot整合swagger2
平时项目开发必须要求写接口文档。之前开发过程中,接口文档的编写是用postman,一边测试接口,一边可以后期生成web文档。但是有几个缺点
第一是web文档加载速度特忙,第二是。。接口太多了,感觉写接口跟写接口文档花的时间都基本差不多了! 然后决定把我项目的swagger配置打开。。
优缺点
swagger就不用简介了,这东西就是扫描你的接口跟注解,动态生成接口文档,它的好处就是动态生成,特别方便。配置完就行了,反正。。平时开发的时候接口也需要写注释
缺点的话也挺明显。首先这个东西会影响你项目的启动速度(上线的环境可以把注解去掉。)、其次,它要配置很多注解,会导致页面看起来很难看,所以我一般把参数描述什么,都直接写在接口描述注解里面
。反正前端嘛。。你懂的
依赖的jar包
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
springfox-swagger-ui
这个包是用来生成图形界面的,否则都是一堆json字符串
新建一个配置类
package cn.eangaie.appcloud.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.Collections;
/**
*@描述 Swagger配置类
*@创建人 Eangaie
*@创建时间 2019/7/8
*/
@Configuration
@EnableSwagger2
public class MySwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"xxx接口文档",
"好好学习,天天向上!",
"API V1.0",
"http://localhost/",
new Contact("ean", "http://www.xxxxx.cn", "18719394156@163.com"),
"Apache", "http://www.apache.org/", Collections.emptyList());
}
}
我比较懒,配置能少就少。这个配置类还可以配置一些错误返回相关等等,自行上google找一下相关文档就有了。反正能启动就行
接口注解使用
@Api(tags = "代理商控制接口")
,控制器类标签注解,打在控制器类上面@ApiOperation("后台代理商授权或续期(默认加7天+10个名额)")
,接口注解,写在这里的文字会成为你接口的描述- 还有很多的注解使用。。参考这个