Swagger介绍与使用

一、Swagger简介

        Swagger是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTfu风格的web服务。目标是使客户端和文件系统作为服务器一同样的速度来更新文件的方法,参数和模型紧密集成到服务器。这个解释简单点来讲就是说,swagger是一款可以根据restful风格生成的接口开发文档,并且支持做测试的一款中间软件。
要从前后端分离讲起

**前后端分离:**VUE+SpringBoot 基本上都用这一套

**后端时代:**前端只用管理静态页面,html===>后端,使用模版引擎 jsp=>后端主力

前后端分离时代:

后端:后端控制层,服务层,数据访问层【后端团队】
前端:前端控制层,视图层,【前端团队】
伪造后端数据,json,已经存在数据,不需要后端,前端工程依旧可以跑起来
前后端如何交互 ====>API
前后端相对独立,松耦合
前后端甚至可以部署在不同的服务器上
产生一个问题:

前后端联调,前端和后端人员无法做到及时协商,解决问题,导致问题爆发
需要一个东西可以解决这个问题
解决问题:

首先指定计划,实时更新API,较低集成风险
早些年:指定word计划文档
前后端分离:
前端测试后端接口:postman
后端提供接口,需要使用更新最新的消息及改动!

官网:https://swagger.io/

Swagger:

号称世界上最流行的api框架
Restful Api文档在线自动生成工具==>api文档和api定义开发
直接运行,可以在线测试api接口;
执行多种语言(c#,java,php)
在项目中使用Swagger需要Springfox

二、导入依赖坐标

Springfox Swagger UI
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

Springfox Swagger2
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<!-- 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>3.0.0</version>
</dependency>

三、 环境介绍

操作系统:macOS Catalina 10.15.3
IDEA:IntelliJ IDEA 2019.1 (Ultimate Edition)
JDK:1.8
SpringBoot:2.2.6RELEASE
Swagger UI:2.9.2
Swagger2:2.9.2


四、 SpringBoot 集成 Swagger
 

1、 新建 SpringBoot 的 Web 项目


2 、导入上述2个依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

3、开启 Swagger

新建一个 SwaggerConfig 类

添加 @Configuration:表明这是一个配置类

开启 Swagger2

@Configuration  //表明这是一个注解类
@EnableSwagger2  //开启 Swagger2
public class SwaggerConfig {

}


4、测试 Swagger2 是否启动成功
访问:http://localhost:8089/swagger-ui.html


5、配置 Swagger 信息

1、编写配置类

package com.kuang.swagger.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 spri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值