如何以编程方式调用swagger codegen? [英] How to call swagger codegen programatically?
问题描述
我正在使用swagger-codgen-cli.jar生成一个安静的java jax-rs api。
现在我用一些命令行选项调用java -jar来执行此操作。
I am generating a restful java jax-rs api with swagger-codgen-cli.jar.
Right now I call java -jar with some command line options to do this.
java -jar swagger-codegen-cli.jar generate -i api.yaml -l jaxrs -o ./outputdir
哪个工作正常。
但是我想从Java程序调用,即将codegen.jar包含到我的类路径中,然后使用类似的参数调用相应的方法。
But I would like to make this call from of a Java program i.e. including the codegen.jar into my classpath and then call the corresponding method with similar parameters.
那么我可以调用swagger-codegen模块的公共API吗?
So is there a public API from the swagger-codegen module which I can call?
推荐答案
如果我正确理解了你需要的东西,你想要动态地生成你的存根类。那么为什么不使用 swagger-codegen-maven-plugin 来生成你的存根类?
If I correctly understand what you need, you would like to dinamically generate your stub classes. So why don't use swagger-codegen-maven-plugin to generate your stub classes?
如使用部分所述,只需添加到build-> plugins部分(默认阶段为generate-sources阶段)
As reported in the usage section, simply add to your build->plugins section (default phase is generate-sources phase)
<plugin>
<groupId>com.garethevans.plugin</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/resources/api.yaml</inputSpec>
<language>java</language>
</configuration>
</execution>
</executions>
</plugin>
如果您想从程序执行命令,可以使用运行时.getRuntime()。exec()
或 Runtime.getRuntime()。 exec()替代方案
If you would like to execute the command from a program you may use Runtime.getRuntime().exec()
or Runtime.getRuntime().exec() alternatives
这篇关于如何以编程方式调用swagger codegen?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!