How to call swagger codegen programmatically?

十年热恋 提交于 2019-12-22 09:37:55

问题


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

Which works fine.

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.

So is there a public API from the swagger-codegen module which I can call?


回答1:


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?

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>

If you would like to execute the command from a program you may use Runtime.getRuntime().exec() or Runtime.getRuntime().exec() alternatives



来源:https://stackoverflow.com/questions/32863130/how-to-call-swagger-codegen-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!