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

如何修复openapi-generator-maven-plugin的io.swagger.annotations包不存在问题?

修复方案

问题原因

你引入的是Swagger/OpenAPI 3.x的注解依赖(io.swagger.core.v3:swagger-annotations),但jaxrs-spec生成器默认生成的是Swagger 2.x的注解代码(依赖io.swagger.annotations包),两者包路径不匹配导致编译报错。


方案一:生成OpenAPI 3.x注解(推荐)

修改openapi-generator-maven-plugin的配置,添加useSwagger3Annotations=true配置项,让生成器输出适配Swagger 3.x的注解代码:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.3.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/openapi.json</inputSpec>
                <generatorName>jaxrs-spec</generatorName>
                <configOptions>
                    <sourceFolder>src/gen/java/main</sourceFolder>
                    <!-- 启用OpenAPI 3.x注解生成 -->
                    <useSwagger3Annotations>true</useSwagger3Annotations>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

修改后生成的代码会使用io.swagger.v3.oas.annotations下的注解,与你当前引入的swagger-annotations 2.2.7依赖完全兼容。

方案二:改用Swagger 2.x注解依赖

若需保留生成的Swagger 2.x注解代码,替换现有依赖为Swagger 2.x版本的swagger-annotations:

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>1.6.6</version>
</dependency>

该版本依赖包含io.swagger.annotations包,可直接解决编译报错。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 21:40:58