RESTful API documentation with springdoc-openapi library in Spring Boot application

In the previous tutorial, I showed you how to use the Springfox library to create RESTful API documentation according to OpenAPI specification in Spring Boot applications. But Springfox has been out of date for a long time and the springdoc-openapi library is updated continuously and has the same function as Springfox, so you should use the springdoc-openapi library from now on. In this tutorial, I will show you how to create RESTful API documentation with springdoc-openapi library in the Spring Boot application.

First, I will create a new Spring Boot project with Spring Web dependency as an example.

The result is as follows:

I will add Springdoc OpenAPI Starter WebMVC UI dependency as follows:

If you are using Spring WebFlux, add the following dependency for it:

Suppose now that I also have a controller that defines an API to manage student information simply as follows:

If you now run this Spring Boot application and access the address http://localhost:8080/swagger-ui/index.html, you will see the same result as when we use Springfox as follows:

By default, springdoc-openapi will scan all the controllers presented in our application to generate documents for the defined APIs.

Similar to the Docket class in Springfox, springdoc-openapi defines a GroupedOpenApi class that helps us configure information to generate API documentation, for example as follows:

The “group” information is mandatory information you need to declare when defining beans using the GroupedOpenApi class. Similar to Springfox, springdoc-openapi also allows us to scan API filters by package or by context path.

As a result, you will see the “group” section will be displayed in the “Select a definition” field as follows:

You can use the Customizers classes located in the “org.springdoc.core.customizers” package of springdoc-openapi, to customize the API documentation the way you want.

For example, I want to change the title displayed for my group “Huong Dan Java” above from “OpenAPI definition” to “Student API”, I will add OpenApiCustomizer as follows:

Result:

We can also customize the document for the API by adding some description about it, the return data, similar to Springfox.

Examples are as follows:

The result is similar to when we use Springfox, guys:

Add Comment