Introduction
Prerequisites
Before we begin, ensure you have the following:
- Basic knowledge of Spring Boot and RESTful APIs.
- JDK installed on your machine (Java 8 or higher).
- Maven or Gradle for dependency management.
- IDE (Eclipse, IntelliJ IDEA, etc.) installed.
Step 1: Setting Up a Spring Boot Project
Let's start by setting up a new Spring Boot project using Spring Initializr. Follow these steps:
Go to Spring Initializr.
Fill in the project metadata:
- Project: Maven or Gradle (choose your preferred build tool).
- Language: Java.
- Spring Boot Version: Choose the latest stable version.
- Group:
com.example
(or your preferred group identifier). - Artifact:
swagger3-demo
(or your preferred artifact name). - Dependencies: Add
Spring Web
andSpringfox
for Swagger 3 integration.
Click on Generate to download the project zip file.
Extract the downloaded zip file and open it in your preferred IDE.
Step 2: Configuring Swagger 3 in Spring Boot
Now, let's configure Swagger 3 in our Spring Boot application. We'll create a configuration class to customize Swagger settings and enable API documentation generation.
Create SwaggerConfig.java
Configuration Class
javapackage com.example.swagger3demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; 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.OAS_30) .select() .apis(RequestHandlerSelectors.basePackage("com.example.swagger3demo.controllers")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger 3 Demo API") .description("API documentation using Swagger 3 in Spring Boot") .version("1.0.0") .build(); } }
Explanation:
@Configuration
: Marks the class as a Spring configuration class.@EnableSwagger2WebMvc
: Enables Swagger 3 integration with Spring MVC.@Bean public Docket api()
: Configures aDocket
bean that specifies the Swagger settings..select()
: Initializes a builder for the API selector..apis(RequestHandlerSelectors.basePackage("com.example.swagger3demo.controllers"))
: Specifies the base package where Spring will scan for API controllers..paths(PathSelectors.any())
: Specifies which paths to include in the documentation (here, all paths are included)..apiInfo(apiInfo())
: Provides metadata about the API (title, description, version).
Step 3: Creating API Controllers with Swagger Annotations
Let's create a simple API controller and annotate it with Swagger 3 annotations to document our APIs.
Create DemoController.java
javapackage com.example.swagger3demo.controllers; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") @Tag(name = "Demo API Controller", description = "Operations pertaining to demo API") public class DemoController { @GetMapping("/hello") @Operation(summary = "Get hello message") public String hello() { return "Hello, Swagger 3!"; } }
Explanation:
@RestController
: Marks the class as a controller that handles RESTful requests.@RequestMapping("/api")
: Maps all API endpoints in this controller to/api
.@Tag(name = "Demo API Controller", description = "Operations pertaining to demo API")
: Provides a tag for grouping related operations in Swagger UI.@GetMapping("/hello")
: Maps HTTP GET requests on/api/hello
to thehello()
method.@Operation(summary = "Get hello message")
: Provides a summary for thehello()
operation in Swagger UI.
Step 4: Running and Testing Swagger UI
Now, let's run our Spring Boot application and test the Swagger 3 integration by accessing Swagger UI in a web browser.
Run the Spring Boot application:
- Open your IDE.
- Right-click on the main class (
Swagger3DemoApplication
) and select Run As > Spring Boot App.
Open a web browser and navigate to
http://localhost:8080/swagger-ui/index.html
.- You should see Swagger UI loaded with your API documentation.
Explore the documented APIs:
- Expand the
Demo API Controller
tag. - Click on
GET /api/hello
to view details about thehello()
operation, including parameters and responses.
- Expand the
Conclusion
In this tutorial, we've covered how to integrate Swagger 3 with a Spring Boot application for automated API documentation generation. By following these steps, you can enhance your API development process with clear and interactive documentation that improves collaboration and understanding among team members.
Additional Resources
Final Thoughts
Effective API documentation is essential for building and maintaining successful software projects. With Swagger 3 and Spring Boot, you can streamline the documentation process and ensure that your APIs are well-documented and easy to understand. Happy documenting!
0 Comments