You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

springdoc-openapi-ui与openapi-generator-maven-plugin不兼容问题咨询

适配解决方案

无需更换代码生成器,你当前使用的openapi-generator-maven-plugin:5.1.0本身就支持直接生成适配springdoc-openapi-ui的注解代码,仅需调整插件配置即可完成自动适配,无需手动修改生成后的注解:

核心配置调整

在项目pom.xml的openapi-generator-maven-plugin配置节点下,新增configOptions参数:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.1.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <!-- 原有配置:spec路径、生成包名、generatorName等保持不变 -->
                <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>com.example.api</apiPackage>
                <modelPackage>com.example.model</modelPackage>
                <!-- 新增以下配置项 -->
                <configOptions>
                    <!-- 核心开关:生成Swagger V3版本注解 -->
                    <useSwaggerV3Annotations>true</useSwaggerV3Annotations>
                    <!-- 关闭Swagger UI相关生成配置,使用springdoc的UI即可 -->
                    <useSwaggerUI>false</useSwaggerUI>
                    <!-- 其余原有configOptions配置保持不变 -->
                    <interfaceOnly>true</interfaceOnly>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

验证适配效果

调整配置后重新执行代码生成命令:
mvn clean openapi-generator:generate

生成的接口和模型类会自动使用io.swagger.v3.oas.annotations.*包下的对应注解,和Spring Boot 2.5.4版本集成的springdoc-openapi-ui完全兼容,无需额外手动迁移。

注意事项

  • Spring Boot 2.x版本配套使用的springdoc-openapi-ui需选择1.x分支版本,不要使用适配Spring Boot 3的2.x分支版本,避免出现兼容性问题。
  • 如果有自定义注解生成需求,可以通过修改openapi-generator的mustache模板实现更细粒度的适配,不需要修改生成后的业务代码。

内容的提问来源于stack exchange,提问作者1174

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 14:39:01