1

I need to consume a REST API created by another team. They have provided me a swagger-ui endpoint. I'm coding in Java. I am using SpringFramework. I'm looking for something that generates stubs that I can implement and schema classes.

1 Answers1

0

Swagger Codegen has a Maven plugin. It can generate Spring server stubs and Java client SDKs among other things.

Codegen has two versions, 3.x and 2.x. They have different groupId.

Version 3.x is for OpenAPI 3.0 definitions (openapi: 3.0.x):

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.27</version>

    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>src/main/resources/api.yaml</inputSpec>
                <language>java</language>
                <configOptions>
                   <sourceFolder>src/gen/java/main</sourceFolder>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

Version 2.x is for OpenAPI 2.0 definitions (swagger: '2.0').

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.4.21</version>

    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>src/main/resources/api.yaml</inputSpec>
                <language>java</language>
                <configOptions>
                   <sourceFolder>src/gen/java/main</sourceFolder>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

For the supported <language> values, paste your OpenAPI YAML/JSON file into https://editor.swagger.io and check the values in the "Generate Server" and "Generate Client" menus.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • I believe that this answer is for generating the code for a server from an OpenAPI specified as YAML. I need to generate the code for a client where I have been given a swagger URL. – Ray Hallquist Sep 23 '21 at 23:37
  • Use `java` or whatever output target you want. All code generators require an OpenAPI YAML/JSON file as input, you can export it from Swagger UI as [explained here](https://stackoverflow.com/q/48525546/113116). – Helen Sep 24 '21 at 01:16
  • do you understand that I'm trying to generate the client side from a partner's API. I'll be calling their API. – Ray Hallquist Sep 25 '21 at 03:14
  • `java` generates a Java _client_ SDK. Codegen can generate both servers and clients, the output depends the `` parameter. Some language values indicate servers, others indicate clients. – Helen Sep 25 '21 at 10:46