因为是前后端项目,需要前端的参与,所以一个好看的接口文档非常的重要

1、引入依赖

美化插件其中自带swagger的依赖了

        <dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-spring-boot-starter</artifactId><version>4.3.0</version></dependency>

2、添加配置类

package com.study.question.config;import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.OpenAPIService;
import org.springdoc.core.PropertyResolverUtils;
import org.springdoc.core.SecurityService;
import org.springdoc.core.SpringDocConfigProperties;
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
import org.springdoc.core.providers.JavadocProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;@ConditionalOnClass({OpenAPI.class})
@EnableConfigurationProperties(SwaggerProperties.class)
@ConditionalOnProperty(prefix = "springdoc.api-docs", name = "enabled", havingValue = "true", matchIfMissing = false) // 设置为 false 时,禁用
public class SwaggerConfig implements Serializable {// ========== 全局 OpenAPI 配置 ==========@Beanpublic OpenAPI createApi(SwaggerProperties properties) {Map<String, SecurityScheme> securitySchemas = buildSecuritySchemes();OpenAPI openAPI = new OpenAPI()// 接口信息.info(buildInfo(properties))// 接口安全配置.components(new Components().securitySchemes(securitySchemas));securitySchemas.keySet().forEach(key -> openAPI.addSecurityItem(new SecurityRequirement().addList(key)));return openAPI;}/*** API 摘要信息*/private Info buildInfo(SwaggerProperties properties) {return new Info().termsOfService("有问题请及时联系申智核微服务架构组:HeliosLy").title(properties.getTitle()).description(properties.getDescription()).version(properties.getVersion()).contact(new Contact().name(properties.getAuthor()).url(properties.getUrl()).email(properties.getEmail())).license(new License().name(properties.getLicense()).url(properties.getLicenseUrl()));}/*** 安全模式,这里配置通过请求头 Authorization 传递 token 参数*/private Map<String, SecurityScheme> buildSecuritySchemes() {Map<String, SecurityScheme> securitySchemes = new HashMap<>();SecurityScheme securityScheme = new SecurityScheme().type(SecurityScheme.Type.APIKEY) // 类型.name(HttpHeaders.AUTHORIZATION) // 请求头的 name.in(SecurityScheme.In.HEADER); // token 所在位置securitySchemes.put(HttpHeaders.AUTHORIZATION, securityScheme);return securitySchemes;}/*** 自定义 OpenAPI 处理器*/@Beanpublic OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,SecurityService securityParser,SpringDocConfigProperties springDocConfigProperties,PropertyResolverUtils propertyResolverUtils,Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomizers,Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomizers,Optional<JavadocProvider> javadocProvider) {return new OpenAPIService(openAPI, securityParser, springDocConfigProperties,propertyResolverUtils, openApiBuilderCustomizers, serverBaseUrlCustomizers, javadocProvider);}}

属性类

package com.study.question.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;import javax.validation.constraints.NotEmpty;
import java.io.Serializable;scription:*/
@Data
@EnableConfigurationProperties
@ConfigurationProperties("study.api-docs")
public class SwaggerProperties implements Serializable {private static final long serialVersionUID = 1L;/*** 是否启用*/@NotEmpty(message = "是否启用,不能为空")private boolean enable=false;/*** 标题*/@NotEmpty(message = "标题不能为空")private String title="在线API文档";/*** 描述*/@NotEmpty(message = "描述不能为空")private String description="申智核在线API文档 http://www.shenzhihe.com.cn";/*** 作者*/@NotEmpty(message = "作者不能为空")private String author="HeliosLy";/*** 版本*/@NotEmpty(message = "版本不能为空")private String version="v1.0";/*** url*/@NotEmpty(message = "扫描的 package 不能为空")private String url;/*** email*/@NotEmpty(message = "扫描的 email 不能为空")private String email;/*** license*/private String license;/*** license-url*/private String licenseUrl;}

配置类中添加

study:api-docs:enable: truetitle: 在线API文档description: 在线API文档version: 1.0url: http://www.szh.comemail: adminly@dingtalk.comlicense: MITlicense-url: https://gitee.com/zhijiantianya/ruoyi-vue-pro/blob/master/LICENSEauthor: SYD

3、展示效果

地址访问:http://localhost:8080/doc.html

4、swagger使用

package com.study.question.model.entity;import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;/**
* @author: SYD
* @since: 2024-12-08
* @description:
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_test_1")
@Schema(title="TTest1对象")
public class TTest1 implements Serializable {private static final long serialVersionUID=1L;@TableField("id")private Long id;@TableField("name")@Schema(description="名称")private String name;}
package com.study.question.controller;import com.study.question.model.entity.TTest1;
import com.study.question.model.pojo.R;
import com.study.question.service.QueueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.validation.constraints.NotNull;@RequiredArgsConstructor
@Slf4j
@RestController
public class QueueController {private final QueueService queueService;@GetMapping("/testQueue")@Operation(summary = "查询")private void test(String name){queueService.test(name);}@GetMapping("/add")@Operation(summary = "添加/更新")private TTest1 test(TTest1 tTest1){return new TTest1();}@GetMapping("/get/{id}")@Operation(summary = "查询详情")public TTest1 getById(@NotNull(message = "id不能为null") @Schema(description = "唯一标志") @PathVariable("id") Integer id) {return new TTest1();}
}

5、bug解决

如果运行报错。Failed to start bean ‘documentationPluginsBootstrapper

是因为springboot2.6.x后会有兼容问题,Springboot2.6以后将SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser,导致出错。

所以要么降springboot的版本,要么yml文件加上一个配置。

spring:mvc:pathmatch:matching-strategy: ANT_PATH_MATCHER

配置类SwaggerConfig加上

@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));}

6.如果有鉴权请注意排除路径

      - /doc*/**- /v3*/*/**- /swagger-ui*/**

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

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

相关文章

STM32——CAN总线

STM32——CAN总线 1. CAN总线基础概念 1.1 CAN总线简介 控制器局域网&#xff08;Controller Area Network, CAN&#xff09;是由Bosch公司开发的串行通信协议&#xff0c;专为汽车电子和工业控制设计&#xff0c;具有以下核心特性&#xff1a; 多主控制架构&#xff1a;所有…

什么是数据倾斜?如何优化?

什么是数据倾斜?如何优化? 一、数据倾斜的定义与表现 数据倾斜是指在大规模数据处理系统中,数据分布严重不均匀的现象,导致某些计算节点负载远高于其他节点。这种现象在分布式计算框架(如Hadoop、Spark)和分布式数据库(如Hive、HBase)中尤为常见。 关键特征:少数节点…

大模型数据流处理实战:Vue+NDJSON的Markdown安全渲染架构

在Vue中使用HTTP流接收大模型NDJSON数据并安全渲染 在构建现代Web应用时&#xff0c;处理大模型返回的流式数据并安全地渲染到页面是一个常见需求。本文将介绍如何在Vue应用中通过普通HTTP流接收NDJSON格式的大模型响应&#xff0c;使用marked、highlight.js和DOMPurify等库进…

第11期_网站搭建_极简云 单码网络验证修复版本 虚拟主机搭建笔记

系统搭建环境 1、Nginx 最佳 2、php 7.2 3、MySql 5.6 后台地址 域名/admin 后台账号 admin 密码 123456 我使用宝塔面板的后门校验&#xff0c;没有发现有后门的现象&#xff0c;使用的话&#xff0c;建议再次核查一下。也希望各位 有能力的也核查一下。 夸克网盘下载地址&…

.net ORM框架dapper批量插入

.NET ORM 框架 Dapper 批量插入全解析 在 .NET 开发中&#xff0c;与数据库交互是常见需求。Dapper 作为轻量级的 ORM&#xff08;对象关系映射&#xff09;库&#xff0c;在简化数据库交互方面表现出色。今天我们就来深入探讨 Dapper 实现批量插入的几种方法。 为什么需要批…

虚拟机CentOS 7 网络连接显示“以太网(ens33,被拔出)“、有线已拔出、CentOS7不显示网络图标

文章目录 一、问题描述二、解决方法1、查看网络连接方式2、开启相关服务3、确认虚拟机网络连接 一、问题描述 问题描述&#xff1a;在VmWare中安装CentOS7, 启动后界面不显示网络的图标。 在GONE桌面—》设置中找到网络设置&#xff0c;发现显示线缆已拔出。 二、解决方法 …

安卓Compose实现鱼骨加载中效果

安卓Compose实现鱼骨加载中效果 文章目录 安卓Compose实现鱼骨加载中效果背景与简介适用场景Compose骨架屏与传统View实现对比Shimmer动画原理简介常见问题与优化建议参考资料 本文首发地址 https://h89.cn/archives/404.html 背景与简介 在移动应用开发中&#xff0c;加载中占…

基于C++处理Modbus报文的完整指南

目录 &#x1f4e6; 一、Modbus报文结构解析1. RTU模式帧格式2. TCP模式帧格式 &#x1f527; 二、C实现方案与库选择示例1&#xff1a;libmodbus读取保持寄存器 (TCP) ⚙️ 三、核心处理技术1. 报文构建与发送2. 响应解析与错误处理3. 数据类型转换 &#x1f680; 四、高级应用…

【性能调优系列】深入解析火焰图:从基础阅读到性能优化实战

博客目录 一、火焰图基础&#xff1a;结构与阅读方法二、深入分析火焰图&#xff1a;关键观察点与性能瓶颈识别1. 识别最宽的函数块2. HTTP 请求处理分析3. 数据库操作分析4. 业务逻辑分析 三、性能优化实战&#xff1a;从火焰图到解决方案1. 线程池性能优化2. 数据库访问优化3…

基于 OpenCV 和 DLib 实现面部特征调整(眼间距、鼻子、嘴巴)

摘 要 本文介绍如何利用Dlib面部特征点检测和OpenCV图像处理技术&#xff0c;通过Python实现面部特征的精准调整。我们将以改变眼间距为例&#xff0c;演示包括地标检测、三角剖分变形等关键技术&#xff0c;该方法可扩展至嘴唇、眉毛等面部特征的调整。 技术栈 Python 3.8 …

Spring Data Redis 实战指南

Spring Data Redis 核心特性 Spring Data Redis 是基于 Redis 的 NoSQL 内存数据结构存储解决方案,为 Spring 应用程序提供与 Redis 交互的高级抽象层。其核心架构设计体现了对现代应用需求的深度适配,主要技术特性可归纳为以下维度: 数据结构支持体系 作为多模型数据存储…

AI IDE 正式上线!通义灵码开箱即用

近期&#xff0c;通义灵码AI IDE正式上线&#xff0c;即日起用户可在通义灵码官网免费下载开箱即用。 作为AI原生的开发环境工具&#xff0c;通义灵码AI IDE深度适配了最新的千问3大模型&#xff0c;并全面集成通义灵码插件能力&#xff0c;具备编程智能体、行间建议预测、行间…

如何搭建Z-Blog PHP版本:详细指南

Z-Blog是一款功能强大且易于使用的博客平台&#xff0c;支持PHP和ASP两种环境。本文将重点介绍如何在PHP环境下搭建Z-Blog博客系统&#xff0c;帮助您快速上线自己的个人博客站点。 准备工作 1. 获取Z-Blog PHP版本 首先&#xff0c;访问Z-Blog官方网站下载最新版本的Z-Blog…

App使用webview套壳引入h5(二)—— app内访问h5,顶部被手机顶部菜单遮挡问题,保留顶部安全距离

引入webview的页面添加safeAreaInsets&#xff0c;对weview的webviewStyles做处理 在myApp中改造 entry.vue代码如下 template><view class"entry-page" :style"{ paddingTop: safeAreaInsets.top px }"><web-view :webview-styles"we…

机器学习:支持向量机(SVM)原理解析及垃圾邮件过滤实战

一、什么是支持向量机&#xff08;SVM&#xff09; 1. 基本概念 1.1 二分类问题的本质 在机器学习中&#xff0c;分类问题是最常见的任务之一。最简单的情况就是二分类&#xff1a;比如一封邮件是“垃圾邮件”还是“正常邮件”&#xff1f;一个病人是“患病”还是“健康”&a…

腾讯云V3签名

想要接入腾讯云的Api&#xff0c;必然先按其文档计算出所要求的签名。 之前也调用过腾讯云的接口&#xff0c;但总是卡在签名这一步&#xff0c;最后放弃选择SDK&#xff0c;这次终于自己代码实现。 可能腾讯云翻新了接口文档&#xff0c;现在阅读起来&#xff0c;清晰了很多&…

STM32中自动生成Flash地址的方法

每页大小为 2KB(0x800 字节),地址间隔为 0x800 总地址空间覆盖范围:0x08000000 ~ 0x0803F800(共 256KB) 适用于 STM32 大容量 / 中容量产品(如 F103 系列) 代码如下 // 通用定义(需根据实际页大小调整) #define FLASH_BASE_ADDR 0x08000000 #define FLASH_PAGE_SIZ…

(12)java+ selenium->元素定位大法之By_link_text

1.简介 本章节介绍元素定位中的link_text,顾名思义是通过链接定位的(官方说法:超链接文本定位)。什么是link_text呢,就是我们在任何一个网页上都可以看到有一个或者多个链接,上面有一个文字描述,点击这个文字,就可以跳转到其他页面。这个就是link_Text。 注意:link_t…

Tomcat 线程模型详解性能调优

1. Tomcat I/O模型详解**&#xff08;了解&#xff09;** 1.1 Linux I/O模型详解 I/O要解决什么问题 I/O&#xff1a;在计算机内存与外部设备之间拷贝数据的过程。 程序通过CPU向外部设备发出读指令&#xff0c;数据从外部设备拷贝至内存需要一段时间&#xff0c;这段时间CPU就…

C++课设:智能优惠快餐点餐系统

名人说&#xff1a;路漫漫其修远兮&#xff0c;吾将上下而求索。—— 屈原《离骚》 创作者&#xff1a;Code_流苏(CSDN)&#xff08;一个喜欢古诗词和编程的Coder&#x1f60a;&#xff09; 专栏介绍&#xff1a;《编程项目实战》 目录 一、项目介绍与亮点功能1. 项目背景2.完…