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();
    }
相关推荐
Joy T1 小时前
Agent 确定性上下文设计:从 Prompt 参数注入到能力分层
java·开发语言·微服务·架构·prompt·agent context
m0_587383004 小时前
广州 24 小时自助健身房解决方案技术实现与实战指南
java·spring·小程序·架构·需求分析
·云扬·4 小时前
Prompt 工程与安全:从 System 设计到注入防御
java·人工智能·安全·ai·prompt
专注仿真4 小时前
引擎扩展性与集成设计深度剖析:从 14 个扩展点到 DIS→HLA 演进之路
开发语言·算法·仿真引擎·推演引擎
Best-Wishes5 小时前
科来网络分析系统(技术交流版)使用说明书
开发语言·junit·php
代码不停5 小时前
自动化测试(java)
java·开发语言·selenium·自动化
鬼手点金7 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
爱上网的小舟7 小时前
星际战士2启动错误怎么办?5种游戏报错解决方法
开发语言·php
paopaokaka_luck8 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)
java·人工智能·spring boot·网络协议·数据分析·echarts
阿里嘎多学长8 小时前
2026-10-12 GitHub 热点项目精选
开发语言·程序员·github·代码托管