How to Integrate Swagger (Springfox) with Spring Boot
A Clean, Simple Guide for Beginners
Documenting REST APIs is one of the most important parts of backend development. Instead of writing documentation manually, tools like Swagger allow you to automatically visualize, explore, and test your APIs from a browser.
In this article, we will walk through a simple and clean implementation of Swagger 2 using Springfox in a Spring Boot application.
🌟 What is Swagger?
Swagger is an open-source framework for designing, documenting, and testing REST APIs.
It provides:
-
✔️ Automatic API documentation
-
✔️ Interactive UI for testing endpoints
-
✔️ A live API sandbox
-
✔️ Zero manual documentation effort
Once Swagger is added, API consumers can discover endpoints, test requests, and understand request/response structures without reading any technical document.
🧱 Project Setup — Add Dependencies
Create a Spring Boot project and add the following dependencies in your pom.xml.
This example uses Spring Boot 1.4.1 and Springfox Swagger 2.2.2.
⚙️ Step 2 — Create Swagger Configuration
Create a new configuration class SwaggerConfig.java under com.vinod.test.
This class:
-
Enables Swagger
-
Defines API info (title, description, version, contact, etc.)
-
Sets endpoint filtering
📝 Step 3 — Create a Sample REST Controller
This is a simple GET endpoint that Swagger will automatically document.
🚀 Step 4 — Create Spring Boot Main Class
▶️ Step 5 — Run the Application
Start the Spring Boot app:
Open your browser and go to:
👉 http://localhost:8080/swagger-ui.html
You will see Swagger UI with your GET /api/test endpoint documented and ready to test.
No comments:
Post a Comment