如何解决 openapi-generator-maven-plugin 使用不推荐使用的类? [英] How to resolve that openapi-generator-maven-plugin uses deprecated class?

查看:219
本文介绍了如何解决 openapi-generator-maven-plugin 使用不推荐使用的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 openapi-generator-maven-plugin 与 Spring Boot 2.4.3 结合使用,根据互联网上的一些示例通过 openapi.yaml 中制定的规范生成代码,其中没有示例提供包含所需的完整依赖项.该插件生成一个名为 OpenAPIDocumentationConfig 的类,它内部使用导入的类 springfox.documentation.spring.web.paths.RelativePathProvider.

I try to use openapi-generator-maven-plugin together with Spring Boot 2.4.3 to generate code by the specifications made in an openapi.yaml according to some examples on the internet whereby no example provided the complete dependencies necessary to include. The plugin generates a class named OpenAPIDocumentationConfig which internally uses the imported class springfox.documentation.spring.web.paths.RelativePathProvider.

AFAIK 这个类已被弃用,取而代之的是 DefaultPathProvider 类,但我找不到包含这个新类的存储库.

AFAIK this class is deprecated in favour of class DefaultPathProvider but I can't find a repository with this new class.

这是我的 POM:

<properties>
    <java.version>11</java.version>
    <springdoc.version>1.5.5</springdoc.version>
    <springfox.version>3.0.0</springfox.version>
</properties>

<dependencies>

    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- OpenApi / Swagger -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>${springdoc.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>${springdoc.version}</version>
    </dependency>

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

    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.1.7</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox.version}</version>
    </dependency>

    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.1</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-core</artifactId>
        <version>3.0.0</version>
    </dependency>
</dependencies>

<build>
    <finalName>app</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>5.0.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatorName>spring</generatorName>
                        <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                        <!--<language>java</language>-->
                        <configOptions>
                            <sourceFolder>src/java/main</sourceFolder>
                            <output>${project.build.directory}/generated-sources</output>
                            <!--<output>${project.basedir}/generated-sources</output>-->
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

仅使用 springdoc-openapi-ui 依赖项会导致更多错误:

Using only the springdoc-openapi-ui dependency results in even more errors:

使用此更新的 POM:

With this updated POM:

<properties>
    <java.version>11</java.version>
    <springdoc.version>1.5.5</springdoc.version>
    <springfox.version>3.0.0</springfox.version>
    <openapi-generator.version>3.0.0</openapi-generator.version>
</properties>

<dependencies>

    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- OpenApi / Swagger -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>${springdoc.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>${springdoc.version}</version>
    </dependency>

<!--        <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.1.7</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-core</artifactId>
        <version>3.0.0</version>
    </dependency>
-->
    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.1</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <finalName>app</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>4.3.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <generatorName>spring</generatorName>
                        <inputSpec>${project.basedir}/src/main/resources/openapi.yaml</inputSpec>
                        <!--<language>java</language>-->
                        <configOptions>
                            <sourceFolder>src/java/main</sourceFolder>
                            <output>${project.build.directory}/generated-sources</output>
                            <!--<output>${project.basedir}/generated-sources</output>-->
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

指定要生成的代码的openapi.yaml:

The openapi.yaml specifying the code to be generated:

openapi: 3.0.3
info:
  title: Title
  description: "REST API Dokumentation xxx"
  version: ${artifactId}
  termsOfService: http://swagger.io/terms/
  contact:
    name: API Support
    email: xxx.yyy@zzz.de
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
#externalDocs:
servers:
  - url: http://{domain}:{port}
    description: The local server
    variables:
      domain:
        default: localhost
        description: api domain
      port:
        enum:
          - '8081'
        default: '8081'
paths:
  /api/hello:
    get:
      summary: Says 'hello' to the user.
      description: A test endpoint.
      parameters:
        - in: path
          name: name
          required: true
          schema:
            type: string
            description: The person's name to address to.
      responses:
        '200':
          description: Ok
        '500':
          description: Server error
        default:
          description: Unexpected error
          content:
            application/json:
              schema:
                type: string

推荐答案

在你的 pom 文件中,你包含了所有不同的 openapis,比如 springfoxspringdocswaggerv3 但它不应该是.使用任何一个库.

In your pom file, you are included all different openapis like springfox, springdoc, swaggerv3 but it shouldn't be. Use any one library.

我的建议是使用 springdoc,这是所需的最少配置.

My suggestion is to use springdoc which is required minimal configuration.

删除 pom.xml 中的所有 openapi 依赖项.在 pom.xml 中单独添加以下依赖项.

Remove all openapi dependencies in your pom.xml. Add the below dependencies alone in pom.xml.

<properties>
    <java.version>11</java.version>
    <springdoc.version>1.5.5</springdoc.version>
    <springfox.version>3.0.0</springfox.version>
    <openapi-generator.version>3.0.0</openapi-generator.version>
</properties>

<dependencies>

    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <!-- OpenApi / Swagger -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>${springdoc.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>${springdoc.version}</version>
    </dependency>

    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.1</version>
    </dependency>

    <!-- Lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <finalName>app</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

application.yml

springdoc:
  swagger-ui.path: /app-api-docs.html

在此处访问 spring 文档

http://localhost:8080/app-api-docs.html

这篇关于如何解决 openapi-generator-maven-plugin 使用不推荐使用的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆