Implementing Swagger in Spring Boot

Arijit Das
2 min readJul 21, 2023

--

Swagger is a powerful tool that allows developers to document, visualize, and interact with APIs in a user-friendly manner. In this article, we will explore how to integrate Swagger with a Spring Boot application to automatically generate API documentation and enable an interactive API playground for testing and exploring endpoints.

To implement Swagger in a Spring Boot application using Java code, you can follow these steps:

Step 1: Add Dependencies
In your `pom.xml` file (Maven) or `build.gradle` file (Gradle), add the necessary dependencies for Springfox Swagger:

For Maven:

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

For Gradle:

implementation 'io.springfox:springfox-boot-starter:3.0.0'

Step 2: Enable Swagger Configuration
Create a configuration class to enable Swagger in your Spring Boot application. Annotate this class with `@Configuration` and `@EnableSwagger2WebMvc` (for Spring Boot 2.x and Springfox 3.x). For older versions of Spring Boot and Springfox, use `@EnableSwagger2`.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller")) // Replace with your controller package
.paths(PathSelectors.any())
.build();
}
}

Step 3: Create REST Controllers
Create your REST controllers or APIs in your Spring Boot application. Swagger will automatically detect these controllers and generate the API documentation.

@RestController
public class ExampleController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Swagger!";
}
}

Step 4: Run the Application
After implementing the above steps, run your Spring Boot application. By default, Swagger documentation will be available at: `http://localhost:8080/swagger-ui/`.

You can use the Swagger UI to explore and test your APIs interactively. It provides a user-friendly interface where you can see all your API endpoints, their request/response structures, and even test the APIs directly from the browser.

That’s it! You have now successfully implemented Swagger in your Spring Boot application using Java code. Swagger will help you document and visualize your APIs, making it easier for other developers to understand and consume your services.

Follow for More Click Here

--

--

Arijit Das

Associate Consultant at Infosys | Ex - TCS | Kreeti Technologies | GATE'20 | JAVA Spring Boot Developer