Introduction

Swagger is an essential tool for API documentation, enabling developers to design, build, document, and consume RESTful web services efficiently. Integrating Swagger with Spring Boot using OpenAPI allows you to generate interactive API documentation, making it easy for developers and stakeholders to explore and interact with your APIs. In this guide, we’ll walk through the steps to integrate Swagger in a Spring Boot application using OpenAPI.

Why Use Swagger and OpenAPI with Spring Boot?


Using Swagger with OpenAPI in Spring Boot provides a streamlined way to create clear, interactive, and standardized API documentation. This documentation enhances collaboration, improves the developer experience, and ensures that your APIs are easy to use and understand.

Step 1: Add Swagger and OpenAPI Dependencies

To get started, include the necessary dependencies in your pom.xml file for Maven

Step 2: Configure Swagger in Your Spring Boot Application

Next, you’ll need to set up your OpenAPI configuration in a Spring configuration class. This setup customizes your API documentation with a title, version, description, and groups your API endpoints.

Create a new class named SwaggerConfig:

import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {

    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("public")
                .packagesToScan("com.yourpackage")
                .build();
    }
    }

This configuration file is crucial for customizing and organizing your API documentation. The pathsToMatch method ensures that only the specified endpoints under /api/** are included in the public API documentation.

Step 3: Configure Application Properties in application.properties

In your application.properties file, add the following properties to define the paths for a OpenAPI documentation and Swagger UI:

springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.path=/swagger-ui.html
    

These properties set the paths where the OpenAPI documentation and Swagger UI can be accessed.

Step 4: Accessing Swagger UI in Spring Boot

Once your Spring Boot application is up and running , you can access the Swagger UI by navigating to following url :

The Swagger UI provides a powerful and user-friendly interface where you can interact with and test your API endpoints. It also allows you to explore different API operations and understand the request/response structures.

Benefits of Integrating Swagger with OpenAPI in Spring Boot
Enhanced Collaboration: Swagger’s interactive documentation makes it easier for developers and stakeholders to understand API functionalities.

Time-Saving: Automatically generated documentation reduces manual effort, allowing teams to focus on development.
Standardisation: OpenAPI ensures that your API documentation follows industry standards, making it more reliable and trustworthy.

Conclusion

Integrating Swagger with OpenAPI in a Spring Boot application is a straightforward process that significantly enhances the documentation and usability of your REST APIs. By following the steps outlined in this guide, you can create clear, interactive API documentation that improves the developer experience and fosters better collaboration.