SpringBoot教程(十九) | SpringBoot集成knife4j

ops/2024/10/18 21:24:41/

首先介绍一下Knife4j. 就是一款接口文档框架,跟swagger类似。 但是整合了很多swagger的功能,页面比swagger美观。现在大有取代swagger之势

官方文档地址: https://doc.xiaominfo.com/docs/quick-start

其实主要的集成方式,在文档里都已经描述了,并且我之前也写过集成SpringBoot集成swagger的文章,大同小异。我用的是SpringBoot2. 没用3是因为没装JDK17. 都这个阶段了,还是建议用3.

在唠叨一下,knife4j 对与openapi2和openapi3都支持,我这里选用的是openapi3.

开始集成,老样子,先引入依赖:

<!-- 接口文档 --><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-openapi3-spring-boot-starter</artifactId><version>4.4.0</version></dependency>

然后在配置文件里添加配置:

knife4j:enable: trueopenapi:title: 接口文档description: "接口文档生成"email: ""concat: adminurl: https://docs.xiaominfo.comversion: v4.0license: Apache 2.0license-url: https://stackoverflow.com/terms-of-service-url: https://stackoverflow.com/group:test1:group-name: ai聊天室api-rule: packageapi-rule-resources:- comn.xxx.xx.cms

我这个配置的好像不太对, 但是也无关紧要,能处理。 详细配置可以参考官网上有个gitee上的代码。

然后就是写注解了,需要在Controller上和vo上写注解,注意openapi2和openapi3的注解是不一样的。这里就简单给个例子吧:

java">@RestController
@RequestMapping("/api/faqType")
@Tag(name="FAQ类型相关接口")
public class FaqTypeController {private final FaqTypeBiz faqTypeBiz;// 构造方法注入public FaqTypeController(FaqTypeBiz faqTypeBiz) {this.faqTypeBiz = faqTypeBiz;}@Operation(summary = "保存Faq类型")@PostMapping("/save")public Result save(@RequestBody FaqTypeVO faqTypeVO){faqTypeBiz.saveFaqType(faqTypeVO);return Result.success();}}
java">@Schema(description = "faq类型分页查询参数")
@Data
public class FaqTypePageReqVO extends BasePageVO {/*** 编号*/@Schema(description = "编号")private String code;/*** 标题*/@Schema(description = "标题")private String title;/*** 内容*/@Schema(description = "内容")private String content;/*** 主管部门*/@Schema(description = "主管部门")private String department;}

然后就可以启动了,如果启动报错,这个错误我在swagger里也写过:

spring:  mvc:pathmatch:matching-strategy: ant_path_matcher

文章传送:https://lsqingfeng.blog.csdn.net/article/details/123689652?spm=1001.2014.3001.5502

然后启动项目: 输入: ip:port/doc.html 就可以打开接口文档了,长的比swagger强。

这里在说一下,如果项目中添加了拦截器,就会导致接口文档出不来,就需要放开才行,我之前也讲过这个问题。

传送: https://lsqingfeng.blog.csdn.net/article/details/123678701?spm=1001.2014.3001.5502

在文章的第四部分,就是拦截器和跨域冲突。

我按照文章里的方式试了一下,发现还是不行。 所以如果遇到这个问题的同学,请使用如下最新的解决方式:还是添加配置类

java">@Configuration
@Slf4j
public class SecurityConfiger implements WebMvcConfigurer {@Autowiredprivate VerifyInterceptor verifyInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {InterceptorRegistration registration = registry.addInterceptor(verifyInterceptor);registration.addPathPatterns("/**").excludePathPatterns("/oauth/callback").excludePathPatterns("/doc.html/**").excludePathPatterns("/swagger-resources/**").excludePathPatterns("/error").excludePathPatterns("/webjars/**").excludePathPatterns("/doc.html").excludePathPatterns("/api").excludePathPatterns("/api-docs").excludePathPatterns("/api-docs/**").excludePathPatterns("/doc.html/**").excludePathPatterns("/v2/**").excludePathPatterns("/v3/**").excludePathPatterns("/actuator/**");}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");}}

这个写的比之前的代码多一下,排除支持的也更多一些。

好了,再会!


http://www.ppmy.cn/ops/19420.html

相关文章

Java web应用性能分析之【sysbench基准测试】

Java web应用性能分析之【CPU飙高分析之MySQL】-CSDN博客 Java web应用性能分析之【Linux服务器性能监控分析概叙】-CSDN博客 Java web应用性能分析概叙-CSDN博客 Java web应用性能分析之【基准测试】-CSDN博客 上面基本科普了一下基准测试&#xff0c;这里我们将从sysbench…

数学分析复习:三角函数的周期性

文章目录 三角函数的周期性 本篇文章适合个人复习翻阅&#xff0c;不建议新手入门使用 三角函数的周期性 本节的主题是研究三角函数的周期性&#xff0c;我们之前已经解析地定义三角函数为 cos ⁡ x ∑ k 0 ∞ ( − 1 ) k x 2 k ( 2 k ) ! , sin ⁡ x ∑ k 0 ∞ ( − 1 )…

LeetCode55. 跳跃游戏

LeetCode55.跳跃游戏 思路 可以看我的上篇博客跳跃游戏II 代码 class Solution { public:bool canJump(vector<int>& nums) {for(int i 0, j 0; i < nums.size(); i){if(j < i) return false;j max(j, i nums[i]);}return true;} };

C语言 | Leetcode C语言题解之第45题跳跃游戏II

题目&#xff1a; 题解&#xff1a; int jump(int* nums, int numsSize) {int steps 0;int maxReach 0;int lastJump 0;for (int i 0; i < numsSize - 1; i) {maxReach (i nums[i] > maxReach) ? i nums[i] : maxReach;if (i lastJump) {lastJump maxReach;st…

Spring

Bean的生命周期 ConfigurableApplicationContext是ApplicationContext的子接口&#xff0c;其中扩展了刷新和关闭容器的方法。 bean生命周期的四个步骤&#xff1a;1.实例化2.依赖注入3.后置处理器postProcessBeforeInitialization方法4.初始化&#xff0c;需要通过bean的init…

CentOS yum安装jdk8

在CentOS系统中使用yum命令安装JDK 8&#xff0c;你可以按照以下步骤操作&#xff1a; 更新系统包: 在开始安装之前&#xff0c;建议先更新你的系统包&#xff0c;确保使用的是最新的软件包信息。 sudo yum update 检查已安装的Java版本&#xff08;可选&#xff09;: 如果你不…

异步日志方案spdlog

异步日志方案spdlog spdlog 是一款高效的 C 日志库&#xff0c;它以其极高的性能和零成本的抽象而著称。spdlog 支持异步和同步日志记录&#xff0c;提供多种日志级别&#xff0c;并允许用户将日志输出到控制台、文件或自定义的接收器。 多线程使用和同步、异步日志没有关系是…

油管大V发文8种策略助你走向成功-华媒舍

油管&#xff08;YouTube&#xff09;是全球最大的在线视频分享平台之一&#xff0c;许多人通过在油管上分享视频内容成为了大V&#xff08;影响力大、受众广的创作者&#xff09;。在这篇文章中&#xff0c;我们将为你介绍8种油管大V发文策略&#xff0c;帮助你在油管上走向成…