Java 如何使用 swagger-codegen-plugin (maven) 生成客户端代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49384545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate client code using with swagger-codegen-plugin (maven)?
提问by rajesh reddy SR
I need to generate a server stub code in eclipse using with swagger-codegen-plugin (for maven) . can you please help how to do it ? and what configuration needed for that( in pom.xml).
我需要在 Eclipse 中使用 swagger-codegen-plugin (for maven) 生成服务器存根代码。你能帮我怎么做吗?以及需要什么配置(在 pom.xml 中)。
采纳答案by rajesh reddy SR
I found this answer. You just need to change pom.xml like below.
我找到了这个答案。您只需要像下面那样更改 pom.xml。
pom.xml.
pom.xml。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<version.swagger.codegen>2.2.1</version.swagger.codegen>
<yaml.file>${project.basedir}/src/main/resources/Api.yaml</yaml.file>
<generated-sources-path>${project.build.directory}/generated-sources</generated-sources-path>
<generated-sources-java-path>main/java</generated-sources-java-path>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${version.swagger.codegen}</version>
<configuration>
<inputSpec>${yaml.file}</inputSpec>
<configOptions>
<sourceFolder>${generated-sources-java-path}</sourceFolder>
</configOptions>
<output>${generated-sources-path}</output>
</configuration>
<executions>
<execution>
<id>generate-swagger-spring</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<language>spring</language>
<modelPackage>${project.groupId}.swagger.model</modelPackage>
<apiPackage>${project.groupId}.swagger.api</apiPackage>
<invokerPackage>${project.groupId}.swagger.invoker</invokerPackage>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-generated-source</id>
<phase>initialize</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${generated-sources-path}/${generated-sources-java-path}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<versionRange>[${version.swagger.codegen},)</versionRange>
<goals>
<goal>generate</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
回答by kolobok
Sample configuration for swagger-codegen-maven-plugin
is available at https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin
示例配置swagger-codegen-maven-plugin
可在https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin 获得
List of possible languages is available here: https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages
可用语言列表可在此处获得:https: //github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages
回答by Cristopher Plasma
<!-- Swagger -->
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>contract-service</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${basedir}/src/main/resources/swagger/rest-data-exchange-format.yaml</inputSpec>
<artifactId>contract-service</artifactId>
<output>${basedir}/target/generated-sources</output>
<language>spring</language>
<modelPackage>ru.payhub.rest.v1.model</modelPackage>
<apiPackage>ru.payhub.rest.v1.api</apiPackage>
<!-- <invokerPackage>ru.payhub.rest.v1.handler</invokerPackage> -->
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<sourceFolder>src/main/java</sourceFolder>
<interfaceOnly>true</interfaceOnly>
<library>spring-boot</library>
<dateLibrary>${generator.datelibrary}</dateLibrary>
<configPackage>ru.payhub.config</configPackage>
<singleContentTypes>true</singleContentTypes>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Official parametrs description here
官方参数说明在这里
Swagger syntax specification here
Swagger 语法规范在这里
On this example maven plugin, used swagger data-model file (yaml) generate model classes for use it in the controllers.
在这个示例 maven 插件中,使用 swagger 数据模型文件 (yaml) 生成模型类以在控制器中使用它。