我想为 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
,但当时不可用。