3

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
MidgarZolom
  • 195
  • 5
  • 15

1 Answers1

4

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

Community
  • 1
  • 1
abarisone
  • 3,707
  • 11
  • 35
  • 54
  • The next step would be to generate an API dynamically yes. But maybe you misunderstood my question. If i understand this plugin correctly, it is a wrapper around swaggers command line options for maven? – Gobliins Sep 30 '15 at 11:14
  • But which is your final aim? Having the stubs created or else? – abarisone Sep 30 '15 at 11:42
  • Aim is: A java program, that runs the swagger-code-generator like the command line. Sthg like SwaggerGenerator gen = new SwaggerGenerator(); gen.setLanguage("jax-rs"); gen.setYaml("...") gen.generateApi() – Gobliins Sep 30 '15 at 12:02
  • Which means, generate the target languages (in this case java jax-rs) java source files from a yaml or json api description. – Gobliins Sep 30 '15 at 12:19
  • I think yes... the stubs created – Gobliins Sep 30 '15 at 15:15