Many teams use OpenAPI to document and consume API services. Using OpenAPI tools we can generate strongly typed clients in a wide range of languages. We can then call other services without needing to maintain type definitions in multiple projects. With Gradle and Spring we can automate the generation and publication of Swagger codegen clients and distribute the packages via AWS S3. This will give us a private artifact repository that we can connect to in calling services.

Annotate your API endpoints

The first step in automating your API client generation is to annotate your API endpoints and generate an OpenAPI spec. In Spring we can use to describe our endpoints. In a file add:

Then add annotations to your controllers:

Generate an openapi.json spec

Next we can use gradle to generate a spec:

Define a custom task to start Spring and write the OpenAPI spec to the build directory.

When we run an file will be written to the directory.

Generate a client

Now we can generate an OpenAPI client using a gradle task:

Run the command like so to write a client into the directory. Next we will publish the client to maven via S3.

Create a maven S3 repository

Create an S3 bucket to use as your maven repository.

Now we can publish to S3 with Gradle.

Publish to S3 using Gradle

We can now use a gradle init script to patch the generated gradle client and add a publishing method for our S3 bucket.

Then to run:

Voila

After this we have shown how to describe your API, generate a client, and publish to S3.