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

使用openapi-generator-maven-plugin生成类时提示Schema类缺少requiredMode()方法

问题

使用openapi-generator-maven-plugin从OpenAPI YML文件生成类时,部分类出现编译错误:

Cannot find symbol symbol: method requiredMode() location: @interface io.swagger.v3.oas.annotations.media.Schema

检查io.swagger.v3.oas.annotations.media.Schema类后,确认该类确实没有requiredMode()方法。

以下是项目的pom.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.maglione</groupId>
<artifactId>contactsapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contactsapi</name>
<description>Demo project </description>
<properties>
    <java.version>17</java.version>
    <openapi-generator.version>6.6.0</openapi-generator.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.6</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>${openapi-generator.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>
                             ${project.basedir}/src/main/resources/contactsapi.yml
                        </inputSpec>
                        <generatorName>spring</generatorName>
                        <apiPackage>com.maglione.contactsapi.api</apiPackage>
                        <modelPackage>com.maglione.contactsapi.model</modelPackage>
                        <supportingFilesToGenerate>
                            ApiUtil.java
                        </supportingFilesToGenerate>
                        <configOptions>
                            <delegatePattern>true</delegatePattern>
                            <useSpringBoot3>true</useSpringBoot3>

                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
解决方案
  • 替换SpringFox为SpringDoc(适配Spring Boot 3)
    SpringFox 3.0.0对Spring Boot 3支持不完善,且依赖的Swagger注解版本较低,无法匹配OpenAPI Generator生成的代码。移除springfox-swagger2依赖,添加SpringDoc依赖:

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.2.0</version>
    </dependency>
    
  • 升级OpenAPI Generator版本
    当前6.6.0版本可能存在与Spring Boot 3适配的小问题,建议升级到最新稳定版,修改pom.properties中的版本:

    <properties>
        <openapi-generator.version>7.6.0</openapi-generator.version>
    </properties>
    
  • 排除冲突的低版本Swagger依赖(临时方案)
    如果无法立刻替换SpringFox,需强制升级swagger-core和swagger-annotations版本,在springfox-swagger2依赖中排除低版本依赖并显式添加高版本:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-annotations</artifactId>
            </exclusion>
            <exclusion>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-models</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.2.15</version>
    </dependency>
    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-models</artifactId>
        <version>2.2.15</version>
    </dependency>
    
  • 清理并重新生成代码
    删除之前生成的类文件,执行mvn clean generate-sources重新生成,确保新依赖生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 02:17:08