简体   繁体   English

如何以编程方式调用 swagger codegen?

[英]How to call swagger codegen programmatically?

I am generating a restful java jax-rs api with swagger-codgen-cli.jar.我正在使用 swagger-codgen-cli.jar 生成一个宁静的 java jax-rs api。
Right now I call java -jar with some command line options to do this.现在我使用一些命令行选项调用 java -jar 来执行此操作。

java -jar swagger-codegen-cli.jar generate -i api.yaml -l jaxrs -o ./outputdir

Which works fine.哪个工作正常。

But I would like to make this call from of a Java program ie including the codegen.jar into my classpath and then call the corresponding method with similar parameters.但是我想从 Java 程序中进行这个调用,即将 codegen.jar 包含到我的类路径中,然后使用类似的参数调用相应的方法。

So is there a public API from the swagger-codegen module which I can call?那么我可以调用来自 swagger-codegen 模块的公共 API 吗?

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?那么为什么不使用swagger-codegen-maven-plugin来生成您的存根类呢?

As reported in the usage section, simply add to your build->plugins section (default phase is generate-sources phase)如使用部分所述,只需添加到您的 build->plugins 部分(默认阶段是 generate-sources 阶段)

<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>

If you would like to execute the command from a program you may use Runtime.getRuntime().exec() or Runtime.getRuntime().exec() alternatives如果您想从程序中执行命令,您可以使用Runtime.getRuntime().exec()Runtime.getRuntime().exec() 替代方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM