springboot3、knife4j-openapi3配置动态接口版本管理

前言:以前用swagger的时候,可以在接口上直接用自定义注解标记版本,但是需要配置Docket,knife4j-openapi3废弃掉Docket换成GroupedOpenApi了,所以需要做一些改动。

java 复制代码
<!-- Knife4j for Spring Boot 3.x -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
    <version>4.5.0</version>
</dependency>
  1. 自定义注解
java 复制代码
package com.xx.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * swagger 版本注解
 *
 * @author maomo
 * @version 1.0.0
 * @date 2022/9/30
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ApiVersion {

    /**
     * 接口版本号(对应swagger中的group)
     * @return String[]
     */
    String[] group();


}

2.1 以前用swagger的时候,swaggerConfig里的写法是:

java 复制代码
    @Bean
    public Docket v100(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("v1.0.0")
                .select()
                .apis(input -> {
                    ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                    return apiVersion != null && Arrays.asList(apiVersion.group()).contains("v1.0.0");
                })//controller路径
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket v110(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("v1.1.0")
                .select()
                .apis(input -> {
                    ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);
                    return apiVersion != null && Arrays.asList(apiVersion.group()).contains("v1.1.0");
                })//controller路径
                .paths(PathSelectors.any())
                .build();
    }

2.2 现在springboot3+knife4j-openapi3的写法是:

java 复制代码
    @Bean
    public GroupedOpenApi V100() {
        return GroupedOpenApi.builder()
                .group("v1.0.0")
                .addOpenApiMethodFilter(method -> {
                    // 获取方法上的 @ApiVersion 注解
                    ApiVersion apiVersion = method.getAnnotation(ApiVersion.class);
                    // 判断是否包含当前版本
                    return apiVersion != null &&
                            Arrays.asList(apiVersion.group()).contains("v1.0.0");
                })
                .packagesToScan("com.wyzz.controller") // 指定扫描的包
                .build();
    }
    @Bean
    public GroupedOpenApi V110() {
        return GroupedOpenApi.builder()
                .group("v1.1.0")
                .addOpenApiMethodFilter(method -> {
                    // 获取方法上的 @ApiVersion 注解
                    ApiVersion apiVersion = method.getAnnotation(ApiVersion.class);
                    // 判断是否包含当前版本
                    return apiVersion != null &&
                            Arrays.asList(apiVersion.group()).contains("v1.1.0");
                })
                .packagesToScan("com.wyzz.controller") // 指定扫描的包
                .build();
    }
相关推荐
asdfg12589636 分钟前
JavaBean是什么?怎么理解?有什么用途?
java·开发语言
dsyyyyy110122 分钟前
JavaScript变量
开发语言·javascript·ecmascript
z落落1 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
allway21 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_462446232 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash
一个梦醒了2 小时前
安装git bash选项推荐
开发语言·git·bash
摇滚侠2 小时前
SpringMVC 入门到实战 文件上传 75-77
java·后端·spring·maven·intellij-idea
GIS数据转换器2 小时前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
ct9782 小时前
React 状态管理方案深度对比
开发语言·前端·react
数量技术宅2 小时前
2026量化前沿:从Reddit热帖到Python实战,如何用赫斯特指数(Hurst)狙击虚假突破?
开发语言·python