7. 实现 API 自动生成

目录

1. pom.xml中引用依赖

2. 引入相关的依赖

3. 编写配置类

4. application.yml 中添加配置

5. API 常用注解 

6. 访问 API 列表

7. API 导入 Postman


使用 Springfox Swagger生成 API,并导入 Postman,完成API单元测试。
Swagger 简介:Swagger 是⼀套 API 定义的规范 ,按照这套规范的要求去定义接口及接口相关信息, 再通过可以解析这套规范工具,就可以生成各种格式的接口文档,以及在线接口调试页面,通过自动文档的方式,解决了接口文档更新不及时的问题。
Springfox 简介:是对 Swagger 规范解析并生成文档的⼀个实现。

1. pom.xml中引用依赖

统⼀管理版本,在 properties 标签中加入版本号:

<springfox-boot-starter.version>3.0.0</springfox-boot-starter.version>

3.0.0 版本对应 Spring Boot 2.6 之前的版本,但是随着 Spring Boot 的更新 Springfox 并没有进行同步的更新,所以存在一些兼容性问题,因此我们选择使用 SpringBoot 2.7.6 版本。

2. 引入相关的依赖

<!-- API⽂档⽣成,基于swagger2 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>${springfox-boot-starter.version}</version>
</dependency>
<!-- SpringBoot健康监控 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3. 编写配置类

  • 在com.bitejiuyeke.forum.config 包下新建SwaggerConfig.java
  • 解决 SpringBoot 2.6.0 以上与 Springfox3.0.0 不兼容的问题,涉及 SpringBoot 版本升级过程中的一 些内部实现变化,具体说明在修改配置文件部分

 将以下代码复制到 SwaggerConfig 类中:

package com.example.demo.controller;import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/*** Swagger配置类*/
// 配置类
@Configuration
// 开启Springfox-Swagger
@EnableOpenApi
public class SwaggerConfig {/*** Springfox-Swagger基本配置* @return*/@Beanpublic Docket createApi() {Docket docket = new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")).paths(PathSelectors.any()).build();return docket;}// 配置API基本信息private ApiInfo apiInfo() {ApiInfo apiInfo = new ApiInfoBuilder().title("论坛系统API").description("论坛系统前后端分离API测试").contact(new Contact("Test","https://hello.fprum.com", "1111111111@qq.com")).version("1.0").build();return apiInfo;}/*** 解决SpringBoot 6.0以上与Swagger 3.0.0 不兼容的问题* 复制即可**/@Beanpublic WebMvcEndpointHandlerMappingwebEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,ServletEndpointsSupplier servletEndpointsSupplier,ControllerEndpointsSupplier controllerEndpointsSupplier,EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,WebEndpointProperties webEndpointProperties, Environment environment) {List<ExposableEndpoint<?>> allEndpoints = new ArrayList();Collection<ExposableWebEndpoint> webEndpoints =webEndpointsSupplier.getEndpoints();allEndpoints.addAll(webEndpoints);allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());String basePath = webEndpointProperties.getBasePath();EndpointMapping endpointMapping = new EndpointMapping(basePath);boolean shouldRegisterLinksMapping =this.shouldRegisterLinksMapping(webEndpointProperties, environment,basePath);return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints,endpointMediaTypes,corsProperties.toCorsConfiguration(), newEndpointLinksResolver(allEndpoints, basePath),shouldRegisterLinksMapping, null);}private boolean shouldRegisterLinksMapping(WebEndpointPropertieswebEndpointProperties, Environment environment,String basePath) {return webEndpointProperties.getDiscovery().isEnabled() &&(StringUtils.hasText(basePath)||ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));}
}

4. application.yml 中添加配置

  • 在 spring 节点下添加 mvc 配置项
由于 SpringBoot 2.6 之后版本把 SpringMVC 路径匹配策略修改为 MatchingStrategy. PATH_PATTERN_PARSER;
而 Springfox-Swagger 还没有更新版本,我们暂时先把 路径匹配策略回退到之前的
MatchingStrategy. ANT_PATH_MATCHER
 mvc:path match:matching-strategy: ANT_PATH_MATCHER #Springfox-Swagger兼容性配置

5. API 常用注解 

  •  @Api: 作用在 Controller 上,对控制器类的说明
    • tags="说明该类的作用,可以在前台界面上看到的注解"
  •  @ApiModel: 作用在响应的类上,对返回响应数据的说明
  •  @ApiModelProerty:作用在类的属性上,对属性的说明
  •  @ApiOperation: 作用在具体方法上,对 API 接口的说明
  •  @ApiParam: 作用在方法中的每⼀个参数上,对参数的属性进行说明

6. 访问 API 列表

启动程序,在浏览器中输入网址:http://127.0.0.1:58080/swagger-ui/index.html

可以正常运行并显示接口信息,说明配置成功,此时接口信息已经显示出来了,可以分别针每个接口进测试,具操作按页面指引即可。

点击“测试接口”出现如下图所示: 

选择对应的 API 列表,点击“Try it out”: 

点击 Execute: 

图中箭头所指方向即为测试结果。

带有参数的同样可以进行测试:

7. API 导入 Postman

获取 API 地址,打开 Swagger 页面的 API 资源地址并复制:

 在 Postman 中导入 url:

接下来输入参数:

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/41692.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

执行Lua脚本后一直查询不到Redis中的数据(附带问题详细排查过程,一波三折)

文章目录 执行Lua脚本后一直查询不到Redis中的数据&#xff08;附带详细问题排查过程&#xff0c;一波三折&#xff09;问题背景问题1&#xff1a;Lua脚本无法切库问题2&#xff1a;RedisTemlate切库报错问题3&#xff1a;序列化导致数据不一致问题4&#xff1a;Lua脚本中单引号…

Etcd备份及恢复

一、Etcd数据备份 1、备份命令 [rootlocalhost ~]# export ETCDCTL_API3 [rootlocalhost ~]# /data/etcd-3.4.9/bin/etcdctl --endpoints10.2.20.108:2379 snapshot save etcd-date "%Y-%m-%d_%H-%M-%S".snapshot 2、备份完成后会在当前目录生成备份文件 [rootlo…

vue实现打印功能

在Vue应用中调用打印机功能&#xff0c;可以使用JavaScript的window.print()方法。这个方法会打开打印对话框&#xff0c;然后让我们选择打印设置并打印文档&#xff0c;但是尼这种方法依赖于浏览器的打印功能。 以下是一个简单的示例&#xff0c;演示如何在Vue组件中调用打印…

Linux Tracing Technologies

目录 1. Linux Tracing Technologies 1. Linux Tracing Technologies Linux Tracing TechnologieseBPFXDPDPDK

Flask Web开发实战(狼书)| 笔记第1、2章

前言 2023-8-11 以前对网站开发萌生了想法&#xff0c;又有些急于求成&#xff0c;在B站照着视频敲了一个基于flask的博客系统。但对于程序的代码难免有些囫囵吞枣&#xff0c;存在许多模糊或不太理解的地方&#xff0c;只会照葫芦画瓢。 而当自己想开发一个什么网站的时&…

ubuntu部署haproxy

HAProxy是可提供高可用性、负载均衡以及基于TCP和HTTP应用的代理. 1、更新系统报 通过在终端中运行以下命令&#xff0c;确保所有系统包都是最新的 sudo apt updatesudo apt upgrade2、安装Haproxy sudo apt install haproxy设置开机自动启动haproxy服务 sudo systemctl en…

Lnton羚通关于如何解决nanoPC-T4 upgrade报错问题?

nanoPC-T4 在 ​​# sudo apt update 和 sudo apt upgrade​​升级或安装软件 ​​sudo apt install xxx​​时遇到以下问题&#xff1a;​​Failed to set up interface with /etc/hostapd/​ Setting up hostapd (2:2.6-15ubuntu2.8) ... Job for hostapd.service failed be…

ssm+vue医院住院管理系统源码和论文PPT

ssmvue医院住院管理系统源码和论文PPT012 开发工具&#xff1a;idea 数据库mysql5.7(mysql5.7最佳) 数据库链接工具&#xff1a;navcat,小海豚等 开发技术&#xff1a;java ssm tomcat8.5 摘 要 随着时代的发展&#xff0c;医疗设备愈来愈完善&#xff0c;医院也变成人们生…

基于IMX6ULLmini的linux裸机开发系列一:汇编点亮LED

思来想去还是决定记录一下点灯&#xff0c;毕竟万物皆点灯嘛 编程步骤 使能GPIO时钟 设置引脚复用为GPIO 设置引脚属性(上下拉、速率、驱动能力) 控制GPIO引脚输出高低电平 使能GPIO时钟 其实和32差不多 先找到控制LED灯的引脚&#xff0c;也就是原理图 文件名 C:/Us…

spring头约束(全部)

文章目录 spring-mvcspring-aopspring-txspring-contextspring-taskspring-cachespring-jdbcp命令空间spring-jeejmslangoxmutil总结 spring-mvc <beans xmlns"http://www.springframework.org/schema/beans" xmlns:xsi"http://www.w3.org/2001/XMLSchema-…

AUTOSAR NvM Block的三种类型

Native NVRAM block Native block是最基础的NvM Block&#xff0c;可以用来存储一个数据&#xff0c;可以配置长度、CRC等。 Redundant NVRAM block Redundant block就是在Native block的基础上再加一个冗余块&#xff0c;当Native block失效&#xff08;读取失败或CRC校验失…

剑指offer44.数字序列中某一位的数字

最后一道题&#xff0c;我一定要自己做出来&#xff0c;想了不到一个小时想法差不多成熟了&#xff0c;但是有一个小细节出问题了&#xff0c;这个问题我在idea上debug都没debug出来。我先讲我的题解然后再讲我这个小问题出在哪里吧。以下是我的代码&#xff1a; class Soluti…

PHP手术麻醉系统源码,自动生成麻醉和护理医疗文书

一套手术麻醉系统源码&#xff0c;可二次开发 手术室麻醉临床信息系统&#xff08;AIMS&#xff09;是应用于医院手术室、麻醉科室的计算机软件系统。该系统针对整个围术期&#xff0c;对病人进行全程跟踪与信息管理&#xff0c;自动集成病人HIS、LIS、RIS、PACS信息&#xff0…

【SA8295P 源码分析】76 - Thermal 功耗 之 /dev/thermalmgr 相关调试命令汇总

【SA8295P 源码分析】76 - Thermal 功耗 之 /dev/thermalmgr 相关调试命令汇总 1、配置文件:/mnt/etc/system/config/thermal-engine.conf2、获取当前SOC所有温度传感器的温度:cat /dev/thermalmgr3、查看所有 Thermal 默认配置和自定义配置:echo query config > /dev/th…

【Spring源码】小白速通解析Spring源码,从0到1,持续更新!

Spring源码 参考资料 https://www.bilibili.com/video/BV1Tz4y1a7FM https://www.bilibili.com/video/BV1iz4y1b75q bean工厂 DefaultListableBeanFactory&#xff08;最原始&#xff09; bean的生命周期 创建&#xff08;实例化&#xff09;–>依赖注入–>-初始化…

利用vue-router跳转的几种方式

​1 <router-link> 2 this.$router.push 跳转到指定路径&#xff0c;并将跳转页面压入history栈中&#xff0c;也就是添加了一个页面记录。3 this.$router.replace 跳转到指定路径&#xff0c;将history栈中的当前页面替换为跳转到的页面。4 this.$router.go(n) 在his…

数据生成 | MATLAB实现WGAN生成对抗网络数据生成

数据生成 | MATLAB实现WGAN生成对抗网络数据生成 目录 数据生成 | MATLAB实现WGAN生成对抗网络数据生成生成效果基本描述程序设计参考资料 生成效果 基本描述 1.WGAN生成对抗网络&#xff0c;数据生成&#xff0c;样本生成程序&#xff0c;MATLAB程序&#xff1b; 2.适用于MATL…

从public static void main(String[] args)看如何构造数据

java语言中public static void main(String[] args)里面的ages有什么作用&#xff1f; 在Java语言中&#xff0c;public static void main(String[] args) 是一个特殊的方法&#xff0c;它是Java程序的入口点。当你运行一个Java程序时&#xff0c;程序会从这个方法开始执行。这…

【游戏评测】河洛群侠传一周目玩后感

总游戏时长接近100小时&#xff0c;刚好一个月。 这两天费了点劲做了些成就&#xff0c;刷了等级&#xff0c;把最终决战做了。 总体感觉还是不错的。游戏是开放世界3D游戏&#xff0c;Unity引擎&#xff0c;瑕疵很多&#xff0c;但胜在剧情扎实&#xff0c;天赋系统、秘籍功法…

kubernetes(二)

文章目录 1. kubernetes常用资源1.1 deployment资源1.2 deployment升级和回滚1.3 tomcat连接mysql1.4 wordpress 2. kubernetes的附加组件2.1 kubernetes集群配置dns服务2.2 kubernetes的dns配置文件2.3 namespace命名空间2.4 kubernetes健康检查2.4.1 健康检查livenessprobo2.…