什么是Swagger?
支持API 自动生成同步的在线文档:使用 Swagger 后可以直接通过代码生成文档,不再需要自己手动编写接口文档了,对程序员来说非常方便,可以节约写文档的时间去学习新技术
使用:
1、新建SpirngBoot项目,导入依赖
<dependencies>
<!-- 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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二、配置SwaggerConfig
package com.tao.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 //开启Swagger
public class SwaggerConfig {
//配置了Swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置Swagger信息=apiinfo
private ApiInfo apiInfo(){
//作者信息
Contact contact= new Contact("涛哥哥", "https://blog.csdn.net/CSDN_429/article/details/124711053", "1362821868@qq.com");
return new ApiInfo(
"涛哥哥的SwaggerAPI文档", //文档开头
"秋刀鱼不会过期", //简介
"1.0", //版本
"https://blog.csdn.net/CSDN_429/article/details/124711053", //路径
contact, //作者信息
"Apache 2.0",
"https://blog.csdn.net/CSDN_429/article/details/124711053", new ArrayList());
}
}
启动测试,访问
http://127.0.0.1:8080/swagger-ui.html
如果网页访问不了,或者控制台报错可以看-》 配置Swagger2控制台报错空指针,访问弹窗口Unable to infer base url. This is common when using dynamic servlet_无糖少冰。的博客-CSDN博客
Swagger配置扫描接口
Docket.select ( )
//配置了Swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// .enable(false) //是否启动Swagger,如果为false,则Swagger不能在浏览器访问
.select()
//RequestHandlerSelectors:配置要扫描接口的方式
//basePackage:指定要扫描的包
//any():扫描全部
//none():不扫描
//withClassAnnotation():扫描类上的注解
//withMethodAnnotation():扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.tao.controller"))
//paths()。过滤什么路径
.paths(PathSelectors.ant("/tao/**"))
.build();
}
.apis(RequestHandlerSelectors.basePackage("com.tao.controller")) 扫描com.tao.controller获取该路径下的全部接口,
.paths(PathSelectors.ant("/tao/**")) 过滤/tao/**下的路径,可以看到controller中并没有该路径,所以不会扫描到接口,效果如下:
如果把过滤去掉就可以扫描到了
controller层
package com.tao.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Swagger {
@RequestMapping("/hello")
public String Swagger(){
return "hello Swagger";
}
}
接口开关,当我们的项目上线的时候,应该把把Swagger关闭
.enable(flage) //是否启动Swagger,如果为false,则Swagger不能在浏览器访问
设置要显示的Swagger环境,通过environment.acceptsProfiles判断是否处在自己设定的环境当中
配置application,properties
spring.profiles.active=dev
config:
@Bean
public Docket docket(Environment environment){
//设置要显示的Swagger环境
Profiles of = Profiles.of("dev", "test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境当中
boolean flage = environment.acceptsProfiles(of);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flage) //是否启动Swagger,如果为false,则Swagger不能在浏览器访问
.select()
//RequestHandlerSelectors:配置要扫描接口的方式
//basePackage:指定要扫描的包
//any():扫描全部
//none():不扫描
//withClassAnnotation():扫描类上的注解
//withMethodAnnotation():扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.tao.controller"))
//paths()。过滤什么路径
// .paths(PathSelectors.ant("/tao/**"))
.build();
}
aip分组,每个组为一个docket,注意不能重名!!!
@Bean
public Docket docket1(Environment environment){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(Environment environment){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3(Environment environment){
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
效果如下:
写实体类以及注释,接口返回实体类会自动扫描到
package com.tao.info;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类") //注释
public class User {
@ApiModelProperty("用户id") //注释
public int id;
@ApiModelProperty("用户名字") //注释
public String name;
}
控制层注释
//只要我们的接口中,返回值中存在实体类,Swagger就会扫描到
@ApiOperation("user接口") //注释
@PostMapping("/user")
public User users(){
return new User();
}
@ApiOperation("hello接口") //注释
@PostMapping("/hello22")
public String users(@ApiParam("用户名")String usename){ //@ApiParam("用户名")注解
return "hello"+usename;
}
效果:
主要强大的功能是可以用来测试接口