24

我想为 OpenAPI 3.0 YAML 文件使用 Swagger Codegen。而且我看到Swagger Codegen 3.0.0-rc0可用。但是当我尝试使用它时,我遇到了问题。以下是详细信息:

我的pom.xml文件带有 swagger-codegen 插件:

<plugin>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.0-rc0</version>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${basedir}/src/main/resources/mySpec.yaml</inputSpec>
        <output>target/generated-sources</output>
        <language>spring</language>
        <generateApis>false</generateApis>
        <modelPackage>com.kj.model</modelPackage>
        <apiPackage>com.kj</apiPackage>
        <configOptions>
          <sourceFolder>swagger</sourceFolder>
          <library>spring-mvc</library>
          <interfaceOnly>true</interfaceOnly>
          <useBeanValidation>true</useBeanValidation>
          <dateLibrary>java8</dateLibrary>
          <java8>true</java8>
        </configOptions>
      </configuration>
    </execution>
  </executions>
</plugin>

当我运行 maven 构建时,使用上面的插件,我得到了这个ServiceConfigurationError,这是堆栈跟踪:

Exception in thread "main" java.util.ServiceConfigurationError: io.swagger.codegen.CodegenConfig: Provider io.swagger.codegen.languages.java.JavaClientCodegen not found
    at java.util.ServiceLoader.fail(ServiceLoader.java:239)
    at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at io.swagger.codegen.CodegenConfigLoader.forName(CodegenConfigLoader.java:19)
    at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:392)
    at io.swagger.codegen.plugin.CodeGenMojo.execute(CodeGenMojo.java:512)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)

为了解决这个问题,我在 pom 文件的 maven 插件部分添加了swagger-codegen-generators依赖项:

<dependencies>
  <dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-generators</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </dependency>
</dependencies>

因此,前面提到的这个问题得到了解决,但现在我看到了这个 NPE

java.lang.NullPointerException
    at io.swagger.codegen.languages.SpringCodegen.preprocessOpenAPI(SpringCodegen.java:429)
    at io.swagger.codegen.DefaultGenerator.configureGeneratorProperties(DefaultGenerator.java:199)
    at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:716)
    at io.swagger.codegen.plugin.CodeGenMojo.execute(CodeGenMojo.java:534)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)

正如您已经注意到的那样,我正在使用<language>spring</language>and <library>spring-mvc</library>。请让我知道 codegen 是否适用于具有这些配置的 3.0.0-rc0 的某人。

注意:我查看了这个类似的旧帖子3.0.0-rc0,但当时不可用。

4

2 回答 2

34

要将 Swagger Codegen 与 OpenAPI 3.0.0 规范的 Maven 插件一起使用,您可以考虑改用OpenAPI Generator(这是 Swagger Codegen 的社区驱动版本)。

<dependency>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>3.3.4</version>
</dependency>

参考:https ://github.com/OpenAPITools/openapi-generator#12---artifacts-on-maven-central

(请参阅关于我们为什么分叉 Swagger Codegen的问答)

于 2018-06-05T15:15:55.530 回答
20

2019 年 4 月发布的 v3 swagger codegen maven 插件从 OpenAPI 3.0 规范生成工作 Java 客户端库,我正在使用这个 Maven pom.xml 插件配置:

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.8</version>
    <executions>
    ..

与 2.4.5 版本相比,所有其余配置和 configOptions 条目均未更改。我必须用以下内容替换旧的注释依赖项,以便客户端代码能够编译:

    <dependency>
        <groupId>io.swagger.core.v3</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>2.0.8</version>
    </dependency>

根据下面@kozla13 的请求,我添加了一个完整的 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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- This groupId will NOT allow deployment in LF -->
    <groupId>org.example.swaggerapi.client</groupId>
    <artifactId>swagger-client</artifactId>
    <name>Example</name>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <client.base.package.name>org.example.so</client.base.package.name>
    </properties>
    <dependencies>
        <!-- Required for Java 9 and later -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
        <!-- HTTP client: Spring RestTemplate -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
        <!-- JSON processing: jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
            <version>2.9.9</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.9</version>
        </dependency>
        <!-- test dependencies -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <!-- This 2019 version is required for OpenAPI 3 -->
                <groupId>io.swagger.codegen.v3</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.8</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/ric-plt-a1/a1/openapi.yaml</inputSpec>
                            <language>java</language>
                            <packageName>${client.base.package.name}</packageName>
                            <modelPackage>${client.base.package.name}.model</modelPackage>
                            <apiPackage>${client.base.package.name}.api</apiPackage>
                            <invokerPackage>${client.base.package.name}.invoker</invokerPackage>
                            <configOptions>
                                <groupId>${project.groupId}</groupId>
                                <artifactId>${project.artifactId}</artifactId>
                                <artifactVersion>${project.version}</artifactVersion>
                                <library>resttemplate</library>
                                <java8>true</java8>
                                <dateLibrary>java8</dateLibrary>
                                <licenseName>Apache 2.0</licenseName>
                                <licenseUrl>https://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
于 2019-05-02T14:52:28.777 回答