Generate Client SDKs with OpenApi Generator in Springboot

Vinayak Ramavath
3 min readJun 30, 2023

--

I’ve been exploring solutions that minimise writing code & found this awesome tool that I’d like to share. You can use the OpenApi Generator to create client SDKs. It takes your OpenAPI spec document as an input and generates clients. It can generate client SDKs in various other languages.

Let’s say you are using REST API-based microservices and would like to call the API of another microservice. You would be using a library like OkHttp, AsyncHttpClient, Jetty, etc. in Java. I can see multiple problems with this approach.

  • You need to maintain API contract information at your end, such as the request body, response body, headers, etc. Now, when some models change, you naturally need to update them at your end.
  • Code duplication in various microservices that use the api contract
  • hard to communicate changes or roll out updates in your api.

To avoid these problems, you can write a client library for your API calls. For example, instead of making an API call to the create user endpoint, you would now be calling the client’s method that does the same:

userClient.createUser(user)

OpenAPI Generator generates this client library automatically using the OpenAPI spec document, and you can import this library and call its methods to make API calls. It uses OkHttp internally for Java.

To do this, you first need an OpenApi Specification Document. You can write this contract yourself or use tools like https://github.com/springdoc/springdoc-openapi-gradle-plugin. You need to add this plugin under plugins section in your build.gradle file.

id "org.springdoc.openapi-gradle-plugin" version "1.6.0"
  • Add the below snippet to the same build.gradle & run ./gradlew clean generateOpenApiDocs to generate the swagger spec file. The file will get generated in your build/docs directory.
openApi {
apiDocsUrl.set("http://localhost:5000/v3/api-docs")
outputDir.set(file("$buildDir/docs"))
outputFileName.set("swagger.yaml")
}
  • Now you have the openapi spec document & you can use this to generate Java client SDK. Once you have added the below snippets to build.gradle run ./gradlew openApiGenerate to generate the client sdk. Your sdk will get generated in the generated-client folder at your project’s root.
  • Add this to build.gradle
plugins {   id "org.openapi.generator" version "6.6.0" }
  • Also add this snippet to build.gradle
openApiGenerate {
generatorName = "java"
inputSpec = "$rootDir/service/build/docs/swagger.yaml".toString()
outputDir = "$rootDir/generated-client"
invokerPackage = "{$group}.client"
apiPackage = "{$group}.client.controllers"
modelPackage = "{$group}.client.models"
groupId = "$group"
id = "client"
version = "1.0-SNAPSHOT"
}

I also went one step ahead and created CI/CD to publish our package to Maven Central (Github Packages) using the below Jenkinsfile. I used an init-client.gradle file where I stored our publishing information and passed this as an input in the gradle publish command.

stage('Git Publisher') {
steps {
sh './gradlew clean generateOpenApiDocs'
sh './gradlew openApiGenerate'
sh 'cd generated-client && chmod +x gradlew && ./gradlew build'
sh 'cp init-client.gradle generated-client/init-client.gradle && cd generated-client && ./gradlew --init-script init-client.gradle publish -PmySecureRepositoryUsername=$GITHUB_USERNAME -PmySecureRepositoryPassword=$GITHUB_TOKEN'
}
}

init-client.gradle File:

// for deploying the generated client
// run in build/generated-client with `./gradlew --init-script init-client.gradle publish`
allprojects {
apply plugin: 'java'
apply plugin: 'maven-publish'
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
repositories {
maven {
name = "GitHubPackages"
url = "<https://maven.pkg.github.com/><Owner_Account>/<repo>"
credentials {
username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
}
}
}
}
}

Looking for more code generation solutions so that developers can focus more on writing business logic or solving real problems and not worry about the nitty-gritty details of code maintenance. Please share if you are using any such software.

--

--