Swagger的作用:生成前后的接口文档:
-
了解Swagger的概念及作用
-
掌握在项目中集成Swagger自动生成API文档
一、SpringBoot集成Swagger
1.依赖:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.10.5</version>
</dependency>
如果出现报错,在application.yaml中配置下面的东西才不会报错:
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher #配置swagger出错时加上的配置;
2.配置swaggerConfig类:
@Configuration //配置类:
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
}
3.访问测试 :http://localhost:8080/swagger-ui.html ,可以看到swagger的界面:
而且Swagger必须是3.0版本以下才可以。这里选用的2.9的版本。
二、配置Swagger
package com.jiang.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
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.apiInfo(apiInfo())
.enable(true) //配置是否启用Swagger,如果是false,在浏览器将无法访问
.select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
.apis(RequestHandlerSelectors.basePackage("com.xxxx.xxx.controller"))//指定controller包
// 配置如何通过path过滤,即这里只扫描请求以/kuang开头的接口
//.paths(PathSelectors.ant("/kuang/**"))
.build();
}
/**
* 配置swaggerInfo信息:
*/
public ApiInfo apiInfo(){
//作者信息:(姓名、邮箱、url)
Contact contact = new Contact("xxx", "www.baidu.com", "xxx@qq.com");
return new ApiInfo(
"Api Documentation", //标签
"Api Documentation", //描述
"1.0", //版本信息
"urn:tos", //组织连接
contact, //联系人信息
"Apache 2.0", //许可
"http://www.apache.org/licenses/LICENSE-2.0", //许可连接
new ArrayList() //扩展
);
}
}
开启版本访问控制:当项目处于test、dev环境时显示swagger,处于prod时不显示?
(1)加注解:@Profile({"dev","test"})
(2)然后在application.yaml中配置:dev或者test都可以 ,如果配置成prod就不能访问
package com.jiang.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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
@Profile({"dev","test"}) //开启版本控制
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.apiInfo(apiInfo())
.enable(true) //配置是否启用Swagger,如果是false,在浏览器将无法访问
.select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
.apis(RequestHandlerSelectors.basePackage("com.jiang.swagger.controller"))
// 配置如何通过path过滤,即这里只扫描请求以/kuang开头的接口
// .paths(PathSelectors.ant("/kuang/**"))
.build();
}
/**
* 配置swaggerInfo信息:
*/
public ApiInfo apiInfo(){
//作者信息:
Contact contact = new Contact("jiangyanming", "www.baidu.com", "1635156042@qq.com");
return new ApiInfo(
"Api Documentation", //标签
"Api Documentation", //描述
"1.0", //版本信息
"urn:tos", //组织连接
contact, //联系人信息
"Apache 2.0", //许可
"http://www.apache.org/licenses/LICENSE-2.0", //许可连接
new ArrayList() //扩展
);
}
}