Swagger UI helps you see and test your API in a simple web page. It makes understanding and using your API easier.
Swagger UI integration in Spring Boot
1. Add dependency in build file (pom.xml for Maven): <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.1.0</version> </dependency> 2. Run your Spring Boot app. 3. Open browser at http://localhost:8080/swagger-ui/index.html
Use the latest version of springdoc-openapi-starter-webmvc-ui for best support.
No extra configuration is needed for basic setup.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>@RestController @RequestMapping("/api") public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Swagger!"; } }
# Run the Spring Boot app and visit: http://localhost:8080/swagger-ui/index.html
This Spring Boot app has one API endpoint /api/hello. After adding the Swagger UI dependency, you can open http://localhost:8080/swagger-ui/index.html to see this endpoint documented and test it interactively.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController @RequestMapping("/api") class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Swagger!"; } }
Swagger UI automatically reads your Spring Boot API endpoints and shows them.
You can customize Swagger UI with properties if needed, but basic use requires no setup.
Make sure your API controllers use standard Spring annotations like @RestController and @GetMapping.
Swagger UI shows your API in a friendly web page for easy testing and documentation.
Add the springdoc-openapi starter dependency to your Spring Boot project to enable it.
Visit /swagger-ui/index.html in your browser to see your API live.
Practice
Solution
Step 1: Understand Swagger UI's role
Swagger UI creates a friendly web interface to view and test API endpoints.Step 2: Compare options
Only To provide a web page that documents and allows testing of your API describes this purpose; others relate to unrelated features.Final Answer:
To provide a web page that documents and allows testing of your API -> Option DQuick Check:
Swagger UI = API documentation and testing [OK]
- Confusing Swagger UI with database tools
- Thinking Swagger handles authentication
- Assuming Swagger generates frontend code
Solution
Step 1: Identify the correct starter for Swagger UI
The springdoc-openapi-starter-webmvc-ui dependency enables Swagger UI integration in Spring Boot.Step 2: Eliminate unrelated dependencies
Other options relate to web server, database, or security, not Swagger UI.Final Answer:
springdoc-openapi-starter-webmvc-ui -> Option AQuick Check:
Swagger UI dependency = springdoc-openapi-starter-webmvc-ui [OK]
- Adding only spring-boot-starter-web without Swagger
- Using database or security starters by mistake
- Forgetting to add any Swagger-related dependency
Solution
Step 1: Recall the default Swagger UI path
springdoc-openapi serves Swagger UI at /swagger-ui/index.html by default.Step 2: Check other options
Other URLs are incorrect or outdated paths for Swagger UI.Final Answer:
/swagger-ui/index.html -> Option CQuick Check:
Swagger UI URL = /swagger-ui/index.html [OK]
- Using /swagger-ui.html which is deprecated
- Confusing API docs JSON URL with UI URL
- Trying /openapi-ui which is invalid
Solution
Step 1: Check common setup issues
After adding a new dependency, the app must be restarted to load it properly.Step 2: Evaluate other options
@EnableSwagger2 is not needed with springdoc-openapi; spring-boot-starter-web is usually present; Swagger UI works on Spring Boot 2.x too.Final Answer:
You need to restart the Spring Boot app after adding dependency -> Option BQuick Check:
Restart app after adding Swagger dependency [OK]
- Thinking @EnableSwagger2 is required with springdoc-openapi
- Missing spring-boot-starter-web (usually present)
- Assuming Swagger UI needs Spring Boot 3 only
Solution
Step 1: Understand customization with springdoc-openapi
Customizing title and description is done by defining an OpenAPI bean with Info details.Step 2: Eliminate incorrect methods
Editing swagger-ui.html is not recommended; @EnableSwagger2 is for older Swagger versions; springdoc.swagger-ui.title property does not exist.Final Answer:
Add a bean of type OpenAPI and set info title and description -> Option AQuick Check:
Customize Swagger UI via OpenAPI bean [OK]
- Trying to edit swagger-ui.html manually
- Using outdated @EnableSwagger2 annotation
- Expecting a springdoc property for title customization
Ready to prove it?
Pass the test and this pattern moves unknown
