Bird
Raised Fist0
Spring Bootframework~30 mins

Swagger UI integration in Spring Boot - Mini Project: Build & Apply

Choose your learning style10 modes available
Swagger UI integration
📖 Scenario: You are building a Spring Boot REST API for a simple product catalog. You want to add Swagger UI to your project so that anyone can easily see and test your API endpoints in a friendly web interface.
🎯 Goal: Integrate Swagger UI into your Spring Boot project so that the API documentation is automatically generated and accessible via a web browser.
📋 What You'll Learn
Add the Swagger dependencies to your project
Create a Swagger configuration class
Configure Swagger to scan your REST controllers
Access the Swagger UI page in the browser
💡 Why This Matters
🌍 Real World
Swagger UI helps developers and testers explore and try out REST APIs easily without writing extra code.
💼 Career
Many companies use Swagger/OpenAPI to document APIs clearly, making this skill valuable for backend and full-stack developers.
Progress0 / 4 steps
1
Add Swagger dependencies
Add the following dependency to your pom.xml file inside the <dependencies> section: <dependency>\n <groupId>io.springfox</groupId>\n <artifactId>springfox-boot-starter</artifactId>\n <version>3.0.0</version>\n</dependency>
Spring Boot
Hint

Make sure to add the dependency inside the <dependencies> tag in your pom.xml.

2
Create Swagger configuration class
Create a Java class named SwaggerConfig in your project under package com.example.config. Annotate it with @Configuration and @EnableOpenApi. Import these annotations from springfox.documentation.oas.annotations.EnableOpenApi and org.springframework.context.annotation.Configuration.
Spring Boot
Hint

This class enables Swagger OpenAPI support in your Spring Boot app.

3
Configure Swagger Docket bean
Inside the SwaggerConfig class, add a public method named api that returns a Docket object. Annotate it with @Bean. In the method, return a new Docket with DocumentationType.OAS_30, and configure it to select any APIs and any paths using select(), apis(RequestHandlerSelectors.any()), and paths(PathSelectors.any()). Import necessary classes from springfox.documentation.spring.web.plugins.Docket, springfox.documentation.spi.DocumentationType, springfox.documentation.builders.PathSelectors, and springfox.documentation.builders.RequestHandlerSelectors.
Spring Boot
Hint

This method tells Swagger to document all API endpoints in your project.

4
Access Swagger UI in browser
Run your Spring Boot application. Open a web browser and go to http://localhost:8080/swagger-ui/index.html to see the Swagger UI page with your API documentation.
Spring Boot
Hint

Make sure your Spring Boot app is running on port 8080 (default) before opening the URL.

Practice

(1/5)
1. What is the main purpose of integrating Swagger UI in a Spring Boot project?
easy
A. To improve database performance
B. To generate frontend UI components
C. To handle user authentication automatically
D. To provide a web page that documents and allows testing of your API

Solution

  1. Step 1: Understand Swagger UI's role

    Swagger UI creates a friendly web interface to view and test API endpoints.
  2. 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.
  3. Final Answer:

    To provide a web page that documents and allows testing of your API -> Option D
  4. Quick Check:

    Swagger UI = API documentation and testing [OK]
Hint: Swagger UI = API docs + testing page [OK]
Common Mistakes:
  • Confusing Swagger UI with database tools
  • Thinking Swagger handles authentication
  • Assuming Swagger generates frontend code
2. Which dependency should you add to a Spring Boot project to enable Swagger UI with springdoc-openapi?
easy
A. springdoc-openapi-starter-webmvc-ui
B. spring-boot-starter-data-jpa
C. spring-boot-starter-web
D. spring-security-oauth2-client

Solution

  1. Step 1: Identify the correct starter for Swagger UI

    The springdoc-openapi-starter-webmvc-ui dependency enables Swagger UI integration in Spring Boot.
  2. Step 2: Eliminate unrelated dependencies

    Other options relate to web server, database, or security, not Swagger UI.
  3. Final Answer:

    springdoc-openapi-starter-webmvc-ui -> Option A
  4. Quick Check:

    Swagger UI dependency = springdoc-openapi-starter-webmvc-ui [OK]
Hint: Use springdoc-openapi-starter-webmvc-ui for Swagger UI [OK]
Common Mistakes:
  • Adding only spring-boot-starter-web without Swagger
  • Using database or security starters by mistake
  • Forgetting to add any Swagger-related dependency
3. Given a Spring Boot app with springdoc-openapi-starter-webmvc-ui added, what URL should you visit to see the Swagger UI page?
medium
A. /openapi-ui/index.html
B. /api-docs/swagger-ui.html
C. /swagger-ui/index.html
D. /swagger-ui.html

Solution

  1. Step 1: Recall the default Swagger UI path

    springdoc-openapi serves Swagger UI at /swagger-ui/index.html by default.
  2. Step 2: Check other options

    Other URLs are incorrect or outdated paths for Swagger UI.
  3. Final Answer:

    /swagger-ui/index.html -> Option C
  4. Quick Check:

    Swagger UI URL = /swagger-ui/index.html [OK]
Hint: Swagger UI default path ends with /swagger-ui/index.html [OK]
Common Mistakes:
  • Using /swagger-ui.html which is deprecated
  • Confusing API docs JSON URL with UI URL
  • Trying /openapi-ui which is invalid
4. You added springdoc-openapi-starter-webmvc-ui but visiting /swagger-ui/index.html shows 404 error. What is the most likely cause?
medium
A. You did not enable @EnableSwagger2 annotation
B. You need to restart the Spring Boot app after adding dependency
C. You forgot to add spring-boot-starter-web dependency
D. Swagger UI is only available in Spring Boot 3.x and above

Solution

  1. Step 1: Check common setup issues

    After adding a new dependency, the app must be restarted to load it properly.
  2. 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.
  3. Final Answer:

    You need to restart the Spring Boot app after adding dependency -> Option B
  4. Quick Check:

    Restart app after adding Swagger dependency [OK]
Hint: Restart app after adding dependencies to avoid 404 [OK]
Common Mistakes:
  • Thinking @EnableSwagger2 is required with springdoc-openapi
  • Missing spring-boot-starter-web (usually present)
  • Assuming Swagger UI needs Spring Boot 3 only
5. You want to customize the Swagger UI page title and description in your Spring Boot app using springdoc-openapi. Which approach is correct?
hard
A. Add a bean of type OpenAPI and set info title and description
B. Edit the swagger-ui.html file directly in resources
C. Use @EnableSwagger2 annotation with title parameter
D. Set springdoc.swagger-ui.title property in application.properties

Solution

  1. Step 1: Understand customization with springdoc-openapi

    Customizing title and description is done by defining an OpenAPI bean with Info details.
  2. 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.
  3. Final Answer:

    Add a bean of type OpenAPI and set info title and description -> Option A
  4. Quick Check:

    Customize Swagger UI via OpenAPI bean [OK]
Hint: Customize Swagger UI by defining OpenAPI bean [OK]
Common Mistakes:
  • Trying to edit swagger-ui.html manually
  • Using outdated @EnableSwagger2 annotation
  • Expecting a springdoc property for title customization
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard