Spring Boot OpenAPI生成器Maven插件

bq3bfh9z  于 2023-04-30  发布在  Spring
关注(0)|答案(1)|浏览(170)

我想尝试使用Openapi生成器maven插件与Sping Boot 项目。
我能用谷歌找到的唯一例子是Baeldung。但是从这个例子中根本不可能弄清楚设置。示例项目可能是超过1000个项目链依赖项的一部分。..
我知道这不是讨论这类问题的论坛,但这一次只有:)有人能给我一个例子,演示谁可以使用Sping Boot 和Java 17的插件。
我得到错误像

package javax.servlet.http does not exist

我最近的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.0.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>

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

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


        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

        <dependency>
            <groupId>jakarta.annotation</groupId>
            <artifactId>jakarta.annotation-api</artifactId>
            <version>1.3.5</version>
        </dependency>

        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
        </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>5.1.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                /Users/ok/dev/openapi/demo/src/main/resources/petstore.yaml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>com.example.demo.api</apiPackage>
                            <modelPackage>com.example.demo.model</modelPackage>
                            <supportingFilesToGenerate>
                                ApiUtil.java
                            </supportingFilesToGenerate>
<!--
                            <configOptions>
                                <delegatePattern>true</delegatePattern>
                            </configOptions>
-->
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
zour9fqk

zour9fqk1#

请试试我的

  • 最近的
  • openapi-v3
  • 示例yaml/json来自petstore。 Swagger (链接如下)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>swagger-codegen-spring</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>swagger-codegen-spring</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>io.swagger.codegen.v3</groupId> <--! v3 -->
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>3.0.42</version>
        <executions>
          <execution>
            <!-- prints (only, additionally) language specific(!) "config options help" -->
            <id>api-generate-help</id> 
            <goals>
              <goal>generate</goal>
            </goals>
            <phase>generate-resources</phase>
            <configuration>
              <inputSpec>src/main/resources/openapi.yaml</inputSpec>
              <!-- language/generator!! -->
              <language>spring</language>
              <configHelp>true</configHelp>
            </configuration>
          </execution>
          <!-- this is the generation: -->
          <execution>
            <id>api-generate</id>
            <goals>
              <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
              <!-- input api definition -->
              <inputSpec>src/main/resources/openapi.yaml</inputSpec>
              <!-- language/generator!! -->
              <language>spring</language>
              <!-- output folder -->
              <output>${project.build.directory}/gen</output>
              <!-- ... -->
              <configOptions> <!-- these are language specific: -->
                <interfaceOnly>false</interfaceOnly>
                <java8>false</java8>
                <dateLibrary>java8</dateLibrary>
                <!--sourceFolder>.</sourceFolder-->
                <throwsException>true</throwsException>
                <useTags>true</useTags>
                <useBeanValidation>true</useBeanValidation>
              </configOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

之后:

mvn generate-sources 
# or idempotent e.g. package, install, ...

我们在(配置的)target/gen文件夹中得到一个“生成的项目”。
该项目是:

    • 非常 * 语言/生成器特定
    • 独立于“父”,并有自己的POM(通常)
  • 我不会将它混入现有POM的(根)目录中

API定义链接:

实时更新

为了公平起见,还有“并发症”:

也“工作”:

<plugin>
  <groupId>org.openapitools</groupId>
  <artifactId>openapi-generator-maven-plugin</artifactId>
  <version>6.5.0</version> <!-- latest -->
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>
          https://petstore3.swagger.io/api/v3/openapi.yaml
        </inputSpec>
        <generatorName>spring</generatorName>
        <output>${project.build.directory}/gen-openapi</output>
      </configuration>
    </execution>
  </executions>
</plugin>

相关问题