一、什么是swagger2
它是一个接口文档----用来前后端分离的一款文档。
二、swagger2的使用
1.引入swagger启动依赖
<!--swagger文档-->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<!--图形化界面-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.7.8</version>
</dependency>
2.创建swagger配置类
package com.gsh.MyConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
/**
* @Auther: haohao
* @Date:2022/7/2221:03
*/
@Configuration//配置文件注解
public class SwaggerConfig {
@Bean
//swagger中所有的功能都封装在Docket类中
public Docket docket(){
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.gsh.controller"))//为指定包下类生成接口文档
.build();
return docket;
}
//自己定义的文档信息
private ApiInfo apiInfo(){
//作者名,作者的联系地址,作者的邮箱
Contact DEFAULT_CONTAC= new Contact("阿豪","http://www.baidu.com","你的邮箱");
//文档,文档简介,文档版本,文档服务器地址,公司名,公司地址
ApiInfo apiInfo=new ApiInfo("在线文档","最牛的文档","v1.0","http://jd.com",
DEFAULT_CONTAC,"啊啊啊工作室","http://www.taobao.com",
new ArrayList<VendorExtension>());
return apiInfo;
}
}
3. 开启swagger注解
@EnableSwagger2//开启swagger2的注解
4.使用swagger注解
@Api 接口类的注解---接口类上 tag属性
@ApiOperation 接口方法的注解---接口方法上 value:
@ApiImplicitParams( 接口参数的说明
{
ApiImplicitParam() //单个参数的说明
}
)
@ApiModel---- 实体类接口注解
@ApiModelProperty---->实体类属性的说明
5.访问swagger
第一种: http://localhost:8080/swagger-ui.html
第二种: http://localhost:8080/doc.html