Swagger3使用教程
由于Swagger2的技术已经落后 {
Swagger2已经在17年停止维护了,取而代之的是 Swagger3 }
我们也需要进行一个更新技术,所以在这里也要交给大家Swagger3的使用
一、了解Swagger
1.Swagger
swagger 是一个 api 文档维护组织,后来成为了 Open API 标准的主要定义者
2.Open API
API的全称是应用编程接口(Application Programming Interface)
在互联网时代,把网站的服务封装成一系列计算机易识别的数据接口开放出去,供第三方开发者使用,这种行为就叫做开放网站的API,与之对应的,所开放的API就被称作Open API。
3.Swagger优点(个人总结)
- RestFul API文档在线生成工具—>>>API文档与API同步更新
- 可以直接运行,可以在线测试API接口
- 支持多种语言:(Java,php…)
二、SpringBoot + Swagger3使用
1.maven依赖的导入
Swagger2需要导入两个资源依赖:
#Swagger2资源依赖
<!-- 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>
Swagger3只需要导入一个资源依赖:
# Swagger3资源依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2.Swagger3的controller创建
package cn.netxiaobao.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@EnableOpenApi //开启Swagger3
@Configuration
public class Swagger3 {
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30) // v2 不同
.select()
.apis(RequestHandlerSelectors.basePackage("cn.netXiaobao.demo"))//设置扫描路径
.build();
}
}
Swagger3的注解和Swagger2的注解一样,不用更换