简体   繁体   中英

How to call swagger codegen programmatically?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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