swagger
前后端分离 swagger是当下最流行的api框架 前后端通过api进行访问 前后端相对独立松耦合
官网:https://swagger.io/
springboot集成swagger
- springfox-swagger2
- swagger-springmvc
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 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>
添加config类
@Configuration //标明这是一个配置类
@EnableSwagger2 //开启Swagger2的自动配置
public class SwaggerConfig {
//配置文档信息
private ApiInfo apiInfo(){
Contact contact = new Contact("联系人名字","https://www.bilibili.com/","1067791265@qq.com");
return new ApiInfo(
"Swagger学习", // 标题
"学习演示如何配置Swagger", // 描述
"v1.0", // 版本
"http://www.bilibili.com/", // 组织链接
contact, // 联系人信息
"Apach 2.0 许可", // 许可
"http://www.apache.org/licenses/LICENSE-2.0", // 许可连接
new ArrayList<>()// 扩展
);
}
//配置Docket实例来配置Swagger的具体参数
@Bean
public Docket docket(Environment environment){
//设置要显示的swagger环境
Profiles of = Profiles.of("dev","test");
//判断当前是否属于该环境
//通过enable() 接收此参数是否要显示
boolean b = environment.acceptsProfiles(of);
return new Docket(DocumentationType.SWAGGER_2)
//.groupName("hello")//配置分组 就是这个swagger是那个项目的
.apiInfo(apiInfo())
.enable(b) //配置是否齐用Swagger
.select()//通过.select()方法 配置接口
.apis(RequestHandlerSelectors.basePackage("com.fan.swagger.controller"))
//RequestHandlerSelectors 配置要扫描接口的方式
//basePackage 指定扫描的包
//any():扫描全服
//none(): 不扫描
//withClassAnnnotation 扫描类上的注解
// 配置如何通过path过滤,即这里只扫描请求以/fan开头的接口
.paths(PathSelectors.ant("/fan/**"))
.build();//工厂模式
}
}
application的配置
# 环境配置 此处指定了dev环境
spring.profiles.active=dev
# 配置swagger
swagger2.enable=true
不同皮肤
- 默认访问http://localhost:8080/swagger-ui.html
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
- bootStrap-ui访问 http://localhost:8080/doc.html
<!-- 引入swagger-bootstrap-ui包 /doc.html-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.1</version>
</dependency>
- layui-ui访问 http://localhost:8080/docs.html
<!-- 引入swagger-ui-layer包 /docs.html-->
<dependency>
<groupId>com.github.caspar-chen</groupId>
<artifactId>swagger-ui-layer</artifactId>
<version>1.1.3</version>
</dependency>
- mg-uimg-ui 访问http://localhost:8080/document.html
<!-- 引入swagger-ui-layer包 /document.html-->
<dependency>
<groupId>com.zyplayer</groupId>
<artifactId>swagger-mg-ui</artifactId>
<version>1.0.6</version>
</dependency>