0
0
Spring Bootframework~5 mins

Swagger UI integration in Spring Boot

Choose your learning style9 modes available
Introduction

Swagger UI helps you see and test your API in a simple web page. It makes understanding and using your API easier.

You want to show your API to other developers clearly.
You want to test your API endpoints without writing extra code.
You want automatic API documentation that updates with your code.
You want to share your API details with your team or clients.
You want to find and fix API issues faster by trying calls directly.
Syntax
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.

Examples
This is the Maven dependency to add Swagger UI support.
Spring Boot
<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  <version>2.1.0</version>
</dependency>
A simple API controller to test Swagger UI.
Spring Boot
@RestController
@RequestMapping("/api")
public class HelloController {

  @GetMapping("/hello")
  public String sayHello() {
    return "Hello, Swagger!";
  }
}
Access Swagger UI in your browser to see and test the API.
Spring Boot
# Run the Spring Boot app and visit:
http://localhost:8080/swagger-ui/index.html
Sample Program

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.

Spring Boot
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!";
  }
}
OutputSuccess
Important Notes

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.

Summary

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.