Generate API client with OpenApi codegen
Tags: tutorial, springboot, openapi, java
Published: 2022-02-01 07:07:06

![client](https://cdn-images-1.medium.com/max/2000/0*Q5BlTDkba1NHrGOO.jpg)

A lot of API are documented using [Swagger](https://swagger.io/), it’s a good thing that API are documented for us developer for understanding how they work and how to call them.
In this article, I will try my best to help you use this documentation in order to generate a client that will call those API.

## What you will need

In order to follow this tutorial, you will need a [REST API](https://medium.com/javarevisited/top-5-books-and-courses-to-learn-restful-web-services-in-java-using-spring-mvc-and-spring-boot-79ec4b351d12?source=---------17------------------), so you can :

* Follow those [tutorial to built your API](https://dev.to/erwanlt/building-a-simple-rest-api-with-springboot-53mc), [one to documente it](https://dev.to/erwanlt/documenting-your-api-with-swagger-2-af6) and then [migrate to openApi](https://dev.to/erwanlt/migration-from-swagger-2-to-openapi-3-1060)

* Having another API documented with openApi 2 ready

## Step 1 : importe the api json documentation

The easy part of this tutorial is to import the json documentation of the api you want the first one to call an put it in your ressource folder.
In my exemple I will use my clone API and I have created a Jedi one, so in order for my clone to retrieve the jedi, i will import the jedi documentation file for then generate it’s client

![](https://cdn-images-1.medium.com/max/2000/1*YsvtAE7hc5lkjajZ5VQJ5g.png)
{% gist https://gist.github.com/ErwanLT/3a0a6438fc1b55ba850fabdbbed32d58.js %}
## Step 2 : Updating our pom.xml

To generate the client we will use the [openApi codegen maven pluggin](https://github.com/OpenAPITools/openapi-generator).

To do that we need to update the build part of our pom.xml and add the pluggin.
{% gist https://gist.github.com/ErwanLT/cc1f1b13c46260ddb17597d958f3db03.js %}
So let’s see what we have here :

* the goal generate indicate us that the client will be generated during the … generate phase of our build, or can be generated using the maven command *mvn generate.*

The configuration will hold some option used to customized the generated client like :

* the inputSpec that tell the pluggin where to find your imported file

* the generatorName for the case

* sourceFolder will be were my client will be generated in my ressource folder

You can add some more options, I recommend you to read the [README.md](https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md) of the project to have a better understanding of the possible options and choose those that applies to your case.

## Step 3 : Launch a build

Now that we have implemented the pluggin usage, let’s go to *clean install* our project.
If all goes right you should have in your target folder, something that look like that :

![](https://cdn-images-1.medium.com/max/2000/1*PU1b0_CtnQpDqHGEuk5JXA.png)

As you can see, at the path described in the pluggin configuration I have my controller JediControllerApi who use ApiClient to call the jedi API and the model described in my documentation Jedi that are generated.

With that I can directly use this object in my project without having to create my own and then do some mapping.

## Step 4 : exploring the generated files

If you have carefully read the step 1, in my documentation file, the server url was my localhost using port 8082, so in my ApiClient i will also have a field (basepath) that will have this value

    private String basePath = “http://localhost:8082";

By default the generated client will use Okhttp to make the api call, so you may have to add some more dependencies to your pom.xml in order to run your app.

## Step 5 : using the client

Now that we have gone all the way through the generation of the client, it will be best to use it don’t you think ?
Nothing really hard here, just declare your generated controller and call it to reach the second API

    JediControllerApi jediControllerApi = **new **JediControllerApi();
    **return **jediControllerApi.getAllJedi();

Thanks for your reading time, as previously, the code used in this tutorial is findable in [this Github repository](https://github.com/ErwanLT/HumanCloningFacilities), branch openApiCodeGen.