Generate API contract using OpenAPI Generator Maven plugin

After defining API specs with RAML, we can use Spring MVC-RAML Maven plugin to generate API contract. With API specs using OpenAPI Specification, you can use OpenAPI Generator Maven plugin to do this. How is it in detail? We will find out together in this tutorial!

First, I will create a new Spring Boot project with Web dependency:

for example.


As an example, I will use the API specs defined in the tutorial Basic definition of RESTful Web Service API specs using OpenAPI Specification. You can get the content of this API specs here, copy all the files and folders into the src/main/resources/api directory of our project:

Now we will declare the basic OpenAPI Generator Maven plugin as follows:

In the configuration section, the <inputSpec> tag will point to the location of the API specs file.

OpenAPI Generator Maven plugin allows us to generate API contracts for many types of projects with different programming languages. You need to declare the Generator name to specify the type of project you want to generate. Generator list here. Here, I have declared to generate a Spring project.

Each Generator defines a lot of different config options, allowing us to generate the project as we want. You declare these config options in the <configOptions> tag. In the above example, I am using the spring generator and I am using two configOptions of this generator, <delegatePattern> and <useSpringBoot3>. The <delegatePattern> option with a value of true will use the Delegate Design Pattern in the implementation for the API specs, separating the interface class from the implementation. And the option <useSpringBoot3> is to generate Spring Boot 3 guys! There are many other config options, you can take a look here.

The <apiPackage> tag is used to declare the package name that the generated Controller classes will use, and <modelPackage> is related to the package that the generated DTO classes will use.

The <output> tag will be the folder containing the generated files. Here I configure this tag with the value “.” to select the current directory. You can use the value “.” or ${project.basedir} is fine.

One point you should note is that when the OpenAPI Generator Maven plugin generates the Spring project for us, it will override the pom.xml file, so please back up the contents of the pom.xml file first. I will tell you how to configure the OpenAPI Generator Maven plugin to skip generate and override the pom.xml file later!

Now, if you run the project with “mvn clean compile”, and refresh the project, you will see the following results:

As you can see, the OpenAPI Generator Maven plugin has generated a Spring project.

Check the pom.xml file, as I said above, you will see that it overrides the pom.xml file as well. To skip generating and override this pom.xml file, please open the .openapi-generator-ignore file in the root directory of the project, and add the line “pom.xml”. Copy the contents of the pom.xml file that you have backed up, the next time you compile, this pom.xml file will not be overridden by the OpenAPI Generator Maven plugin anymore!

Run “mvn clean compile” again, and check the console log, you will see the following files are generated:

The package com.huongdanjava.openapispring.dto will contain the DTOs which are the Schema objects that we defined in the YAML file.

The package com.huongdanjava.openapispring.web will contain Controller classes corresponding to URL requests, organized according to the Delegate Design Pattern. In addition, you will also see an ApiUtil class that defines the utility methods used in the Controller classes.

The org.openapitools package defines another Controller, allowing us to get the contents of the src/main/resources/openapi.yaml file generated by the OpenAPI Generator Maven plugin. The content of this openapi.yaml file is converted from the content of our YAML file, which in this example is student.yaml.

The file is also overridden to add the following properties:

You should also add this file to the .openapi-generator-ignore “src/main/resources/” file to avoid overriding!

Some dependencies need to be added to the pom.xml file for our project to be able to compile, as follows:

You also need to edit the code of the Openapi Generator SpringApplication class so that our application can fully scan the Controllers as follows:

Generated code contains 2 classes with @SpringBootApplication annotation, OpenAPI2SpringBoot and OpenApiGeneratorApplication in addition to the OpenapiGeneratorSpringApplication class above.

Since we are using the OpenapiGeneratorSpringApplication class to run the Spring Boot application, I will comment the @SpringBootApplication annotation and the @ComponentScan annotation in the OpenAPI2SpringBoot and OpenApiGeneratorApplication classes. Then add these 2 classes to the .openapi-generator-ignore file, so that they won’t be re-generated anymore!

Depending on your needs, please configure accordingly!

Now, if you run the application and go to http://localhost:8081/, you will see the following output:

This is the Swagger API documentation! Using it, you will know how many request URLs our application has exposed. The information of each request URL looks like, and we can use this Swagger API documentation to call the actual request URL.

Currently, we have not implemented URL requests. You can add a new class that implements the StudentsApiDelegate interface to do this. My example is as follows:

The result, if we request to the request URL http://localhost:8081/api/students/1 now, is as follows:
If you generate a Spring Webflux project, please add a config option “reactive”:

Of course, then, you must add a Spring Webflux dependency so that the project can be compiled:

Add Comment