配置Api自动生成

news/2025/2/22 1:48:18/

我的飞书:https://rvg7rs2jk1g.feishu.cn/docx/TVlJdMgYLoDJrsxAwMgcCE14nxt

使用Springfox Swagger生成API,并导入Postman,完成API单元测试

Swagger: 是一套API定义的规范,按照这套规范的要求去定义接口及接口相关信息,再通过可以解析这套规范工具,就可以生成各种格式的接口文档,以及在线接口调试页面,通过自动文档的方式,解决了接口文档更新不及时的问题。

Springfox: 是对Swagger规范解析并生成文档的一个实现。

代码配置

因为 Springfox 版本(3.0) 的问题,他的版本只有在 springBoot 2.5x能够进行使用,如果我们这里使用springBoot 2.7x及以上就会出现兼容性问题,此时我们就要进行一些适配操作

这里我使用的springBoot版本为 2.7.6

我们需要在 pom 文件中加入对应依赖

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

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

引入依赖

<!-- API文档生成,基于swagger2 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>${springfox-boot-starter.version}</version>
</dependency>
<!-- SpringBoot健康监控(只是为了方便关注spring状态) -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

编写配置类

将以下代码添加至项目中

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;// 配置类
@Configuration
// 开启Springfox-Swagger
@EnableOpenApi //生成 API 功能
public class SwaggerConfig {/*** Springfox-Swagger基本配置* @return*/@Beanpublic Docket createApi() {Docket docket = new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.example.forum.controller")) //填写自己项目中的controller包路径.paths(PathSelectors.any()).build();return docket;}// 配置API基本信息,以下信息都为自定义信息private ApiInfo apiInfo() {ApiInfo apiInfo = new ApiInfoBuilder().title("") //标题.description("") //描述.contact(new Contact("name", "url", "email")).version("1.0").build();return apiInfo;}/*** 解决SpringBoot 2.6.0以上与Swagger 3.0.0 不兼容的问题* 复制即可**/@Beanpublic WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(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(), new EndpointLinksResolver(allEndpoints, basePath),shouldRegisterLinksMapping, null);}private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment,String basePath) {return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath)|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));}}

修改配置文件

在yml文件中加入新的配置,这里必须加入!!不然会出现不适配的问题,因为使用的SpringBoot相对于Springfox来说是高版本

spring:mvc:path match:matching-strategy: ant_path_matcher

API常用注解以及使用方法

@Api

作用在 Controller 上,对控制器类进行说明

tags = " 说明该类的作用,可以在前台界面上看到的注释 "

@ApiModel

作用在相应的类上,对返回响应数据的说明

@ApiModelProperty

作用在类的属性上,对属性的说明,也就是具体的对象上

@ApiOperation

作用在具体的方法上,对API接口的说明

@ApiParam

作用在方法中的每一个参数上,对参数的属性进行说明

访问API列表

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

进入对应网页

连接 postman

获取 API 地址,获取API资源地址之后复制

打开 postman,选择要复制到的地方.选择 Woekspaces ,选择 My Workspace

在左边工具栏中找到 APIs ,选择 import ,输入刚才复制的网址


http://www.ppmy.cn/news/1574027.html

相关文章

tp6上传文件大小超过了最大值+验证文件上传大小和格式函数

问题&#xff1a; 最近用tp6的文件上传方法上传文件时报文件过大错误。如下所示&#xff1a; $file $this->request->file(file);{"code": 1,"msg": "上传文件大小超过了最大值&#xff01;","data": {"code": 1,&q…

力扣hot100——轮转数组

给定一个整数数组 nums&#xff0c;将数组中的元素向右轮转 k 个位置&#xff0c;其中 k 是非负数。 解法思路&#xff1a; // 空间换时间:轮转问题看作为平移&#xff0c;轮转就是转一圈&#xff0c;如果拉直这个轮的话&#xff0c;其实就是所有数字向后平移k // 建立一个大小…

[Windows] Win7也能控制安卓手机屏幕(手机镜像投屏):scrcpy

Win7也能控制安卓手机屏幕&#xff08;手机镜像投屏&#xff09;&#xff1a;scrcpy 链接&#xff1a;https://pan.xunlei.com/s/VOJGlhQkX9mNqCYsM2cMbYxsA1?pwdm9wq# 系统平台&#xff1a;Windows 7/10/11 &#xff08;Win7系统需打开“Win7”文件夹进行操作&#xff09; …

【ISO 14229-1:2023 UDS诊断(ECU复位0x11服务)测试用例CAPL代码全解析④】

ISO 14229-1:2023 UDS诊断【ECU复位0x11服务】_TestCase04 作者:车端域控测试工程师 更新日期:2025年02月17日 关键词:UDS诊断协议、ECU复位服务、0x11服务、ISO 14229-1:2023 TC11-004测试用例 用例ID测试场景验证要点参考条款预期结果TC11-004无效子功能码检测发送0x11 …

【论文阅读笔记】浅谈深度学习中的知识蒸馏 | 关系知识蒸馏 | CVPR 2019 | RKD

目录 一 方法 1 常规知识蒸馏 2 关系知识蒸馏 2.1 Distance-wise distillation loss 2.2 Angle-wise distillation loss 2.3 Training with RKD 2.4 Distillation target layer 3 实验 论文题目&#xff1a;Relational Knowledge Distillation 论文地址&#xff1a;ht…

【tips】el-select没有赋值但是初始化回显了

页面初始化&#xff0c;下拉框的list是异步的&#xff0c;且没有默认选中值。别的都正常&#xff0c;就是有一个下拉框竟然回显了。。。 原来是这样写的。 <div>serverID-{{form.serverID}}</div> <el-select class"filter-item" v-model"form.…

​44页PDF | 天津大学深度解读DeepSeek:原理与效应(附下载)

一、前言 这份报告深入解读了DeepSeek大语言模型的技术原理、发展效应及未来展望。报告首先回顾了生成式AI的发展历程&#xff0c;从2014年的Attention机制到2024年的o1/R1技术&#xff0c;展示了AI从统计方法到Transformer架构、再到强化学习与推理能力的演进。接着&#xff…

Spring MVC 的核心以及执行流程

Spring MVC的核心 Spring MVC是Spring框架中的一个重要模块&#xff0c;它采用了经典的MVC&#xff08;Model-View-Controller&#xff09;设计模式。 MVC是一种软件架构的思想&#xff0c;它将软件按照模型&#xff08;Model&#xff09;、视图&#xff08;View&#xff09;…