当前位置: 首页 > 知识库问答 >
问题:

如何解决openapi生成器maven-plugin使用已弃用的类?

孟光耀
2023-03-14

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

AFAIK这个类不推荐使用classDefaultPathProvider,但我找不到这个新类的存储库。

这是我的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>

更新后的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:

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

共有2个答案

呼延承平
2023-03-14

在pom文件中,包含了所有不同的OpenAPI,如springfox、springdoc、swaggerv3,但不应该包含在内。使用任意一个库。

我的建议是使用springdoc,它需要最少的配置。

删除pom.xml中的所有openapi依赖项。在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>

应用yml公司

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

在此处访问spring文档

http://localhost:8080/app-api-docs.html
松茂实
2023-03-14

我们可以将useLombok和模板与lombok一起使用

<useLombok>true</useLombok>
<templateDirectory>path to template folder</templateDirectory>

https://github.com/deviantlycan/openapi-generator-templates/tree/master/generator-templates/JavaSpring/spring-boot-lombok-actuator

 类似资料:
  • 我的目标是使用OpenAPI3.0生成Spring Boot REST客户端。 我希望首先生成API的OpenAPI规范文件(springdoc-openapi-maven-plugin),然后使用Maven从该文件(swagger-codegen-maven-plugin)生成客户机代码。 我的问题是swagger-codegen-maven-plugin在springdoc-openapi-m

  • 我正在开发一个具有Angular前端和RESTful Spring Boot Backend的应用程序 我从org.openapitools.找到了这个非常方便的maven插件openapi生成器maven插件凭借其代码生成功能,它有助于在我们的应用编程接口的前端和后端之间实施“合同优先”的方法。但是我们的swagger文件在请求体和响应体定义中使用了“oneOf”属性。我试图从中生成Spring

  • 我试图在maven构建期间生成swagger.json文件。 这是我在pom.xml中从docs获得的插件。 任何帮助都将不胜感激。

  • 就在最近,上下文。getResources()。updateConfiguration()在Android API 25中已被弃用,我需要更改用户选择的应用程序的语言。我正在使用此方法更改语言 但由于受到了不推荐的api警告,我刚刚开始android开发。因此,任何建议都将有助于解决此警告。

  • 我对Jaxb和这个maven-jaxb2插件非常陌生 这是我的. xsd文件: 这是我的。xjb文件: 当它生成时,它给出的java类有点像: 但是我不想为用户和用户列表重复相同的代码,我可以通过注释或定义正则表达式来做什么。请建议一些方法。 我只是想知道这段代码要写什么 因此,当它创建用户类时,它会显示@xmlRootElement(name='user'),而其他类的@xmlRootEleme