Swagger3基于SpringBoot的使用教程


由于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优点(个人总结)

  1. RestFul API文档在线生成工具—>>>API文档与API同步更新
  2. 可以直接运行,可以在线测试API接口
  3. 支持多种语言:(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的注解一样,不用更换

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

netXiaobao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值