Spring Boot OpenAPI生成器Maven插件生成缺少符号的类

owfi6suc  于 2023-06-05  发布在  Spring
关注(0)|答案(1)|浏览(524)

我使用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 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>

我试图在网上搜索可能的错误,但没有找到任何东西

kyvafyod

kyvafyod1#

欢迎来到StackOverflow,* 杰克尼克 *!
正如 Helen 提到的,requiredMode()只存在于swagger-annotations的新版本中。要检查项目使用的版本,请运行mvn dependency:tree。您会发现使用的是swagger-annotations版本2.1.2,通过springfox-swagger2传递包含。
如果从pom.xml中删除supportingFilesToGenerate部分,openapi-generator将创建一个文件.target/generated-sources/openapi/pom.xml,告诉您要与生成的代码一起使用的预期依赖项。在您的示例中,这些依赖项包括io.swagger.core.v3:swagger-core-jakarta:jar:2.2.7,通过org.springdoc:springdoc-openapi-starter-webmvc-ui:jar:2.0.2传递。
要修复您的构建,您需要包含较新的swagger依赖项。您还需要排除旧的swagger依赖项,以便maven知道它必须使用新依赖项中的注解,而不能使用旧依赖项中的注解。

因此,要解决您的问题,请替换以下内容:

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>

通过:

<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>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-core-jakarta</artifactId>
            <version>2.2.7</version>
        </dependency>

相关问题