SpringDoc OpenAPI helps you automatically create clear API documentation for your Spring Boot apps. It makes it easy for others to understand and use your APIs.
SpringDoc OpenAPI setup in Spring Boot
1. Add SpringDoc dependency in your build file (Maven or Gradle). 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. Access the API docs at: http://localhost:8080/swagger-ui/index.html (Optional) Customize OpenAPI info by creating a @Bean of type OpenAPI.
Use the latest version of springdoc-openapi-starter-webmvc-ui for best support.
The default URL for the Swagger UI is /swagger-ui/index.html.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info().title("My API").version("1.0").description("Simple API docs"));
}This Spring Boot setup adds SpringDoc OpenAPI with a custom title and a simple REST endpoint. When you run the app, you can visit /swagger-ui/index.html to see the API docs and test the /hello endpoint.
package com.example.demo; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @Configuration public class OpenApiConfig { @Bean public OpenAPI customOpenAPI() { return new OpenAPI() .info(new Info().title("Demo API").version("1.0").description("Demo API documentation")); } } @RestController class HelloController { @GetMapping("/hello") public String hello() { return "Hello, SpringDoc!"; } }
SpringDoc automatically scans your REST controllers to generate docs.
You can customize many parts of the OpenAPI spec by adding more info in the OpenAPI bean.
Make sure your app runs on port 8080 or adjust the URL accordingly.
SpringDoc OpenAPI creates live API docs for Spring Boot apps automatically.
Add the springdoc-openapi-starter-webmvc-ui dependency to enable it.
Customize docs by defining an OpenAPI bean with your API info.
Practice
springdoc-openapi-starter-webmvc-ui dependency in a Spring Boot project?Solution
Step 1: Understand the role of springdoc-openapi-starter-webmvc-ui
This dependency integrates SpringDoc OpenAPI with Spring Boot to generate live API docs automatically.Step 2: Identify the purpose of the dependency
It provides a user interface and backend support to display API documentation without manual setup.Final Answer:
To automatically generate live API documentation for the application -> Option CQuick Check:
SpringDoc dependency = live API docs [OK]
- Confusing it with database or security dependencies
- Thinking it configures logging
- Assuming manual doc writing is still needed
Solution
Step 1: Recall the OpenAPI bean syntax
The OpenAPI bean requires an Info object with title and version set via methods like title() and version().Step 2: Compare options with correct method names
Only @Bean public OpenAPI customOpenAPI() { return new OpenAPI().info(new Info().title("API").version("1.0")); } uses correct chaining: new OpenAPI().info(new Info().title("API").version("1.0"))Final Answer:
@Bean public OpenAPI customOpenAPI() { return new OpenAPI().info(new Info().title("API").version("1.0")); } -> Option DQuick Check:
OpenAPI bean uses info(new Info().title().version()) [OK]
- Using incorrect method names like setTitle or info(String, String)
- Passing strings directly instead of Info object
- Using non-existent methods like name() or ver()
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}Assuming
springdoc-openapi-starter-webmvc-ui is added as dependency and no custom properties are set.Solution
Step 1: Recall default Swagger UI path with SpringDoc
SpringDoc by default serves Swagger UI at /swagger-ui/index.htmlStep 2: Eliminate other options
/swagger-ui.html is old Swagger path, /api-docs is for JSON docs, /openapi-ui is invalid defaultFinal Answer:
/swagger-ui/index.html -> Option BQuick Check:
Default Swagger UI path = /swagger-ui/index.html [OK]
- Using old Swagger UI path /swagger-ui.html
- Confusing JSON docs path with UI path
- Assuming custom paths without configuration
springdoc-openapi-starter-webmvc-ui dependency but when you run your Spring Boot app, the Swagger UI page shows a 404 error. What is the most likely cause?Solution
Step 1: Check common reasons for 404 on Swagger UI
Missing dependency or wrong annotation usually cause app startup errors, not 404 on UI page.Step 2: Verify URL correctness
Swagger UI default URL is /swagger-ui/index.html, accessing other URLs like /swagger-ui.html causes 404.Final Answer:
You are trying to access the Swagger UI at the wrong URL -> Option AQuick Check:
404 on Swagger UI usually means wrong URL [OK]
- Assuming missing OpenAPI bean causes 404
- Thinking missing @SpringBootApplication causes 404 instead of startup failure
- Ignoring URL path correctness
Solution
Step 1: Ensure the method is a Spring bean
To register the OpenAPI object, the method must be annotated with @Bean inside a configuration class.Step 2: Check the code snippet correctness
@Bean public OpenAPI customOpenAPI() { return new OpenAPI().info(new Info().title("My API").version("2.0")); } correctly uses @Bean annotation and returns new OpenAPI().info(new Info().title("My API").version("2.0"))Final Answer:
@Bean public OpenAPI customOpenAPI() { return new OpenAPI().info(new Info().title("My API").version("2.0")); } -> Option AQuick Check:
Use @Bean annotation to register OpenAPI bean [OK]
- Omitting @Bean annotation so Spring ignores the method
- Using @Component or @Configuration on method instead of class
- Not returning an OpenAPI object with info set
Ready to prove it?
Pass the test and this pattern moves unknown
