Sub topic 6
Describing the API with an OpenAPI bean
This chapter assumes you have a Spring Boot 3 project with the springdoc starter added, and that /v3/api-docs and Swagger UI already respond. You should also be comfortable with @Configuration classes and @Bean methods.
Why add metadata
With no configuration, springdoc fills the top of the document with placeholder values. The title is "OpenAPI definition" and the version is "v0". Readers of Swagger UI and tools that consume the JSON see these defaults first. An OpenAPI bean lets you replace them with real information about your API.
How springdoc uses the bean
springdoc builds the document by scanning your controllers. If the application context contains a bean of type io.swagger.v3.oas.models.OpenAPI, springdoc uses it as the starting object. It then merges the scanned paths and schemas into it. Whatever you set on the bean, such as info, appears in the output. The paths are still generated from your code.
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI libraryOpenApi() {
return new OpenAPI()
.info(new Info()
.title("Library API")
.version("1.0.0")
.description("Manage the book catalog")
.contact(new Contact()
.name("Library Team")
.email("[email protected]")))
.addServersItem(new Server()
.url("http://localhost:8080")
.description("Local development"));
}
}After a restart, open /v3/api-docs. The start of the JSON now contains your values:
{
"openapi": "3.0.1",
"info": {
"title": "Library API",
"version": "1.0.0",
"description": "Manage the book catalog"
}
}Swagger UI shows the same title, version and description in its header.
The pieces of the metadata
| Object | Purpose |
|---|---|
| `Info` | Title, version, description, contact and license of the API |
| `Server` | Base URLs that Swagger UI offers in its server drop-down |
| `ExternalDocumentation` | A link to a guide or website outside the generated document |
Set version to the version of your API contract. It does not have to match the application's build version, though many teams keep them aligned.
Annotation alternative
You can declare the same information with @OpenAPIDefinition on any configuration class. It takes an @Info annotation, as shown in the security example in a later chapter. Use one style per project. If you define both, the bean and the annotation are not combined in a way you should rely on, so keep a single source of truth.
Common errors
- Defining two
OpenAPIbeans. In a single-group setup, Spring may fail to start or springdoc may pick one unpredictably. Declare exactly one. UseGroupedOpenApiwhen you need several documents. - Expecting the bean to list endpoints. Adding paths by hand is rarely needed. Controllers supply them, and your bean only supplies the surrounding metadata.
- A wrong server URL. If Swagger UI sends "Try it out" requests to the wrong host, check
Serverentries. A hard-codedlocalhostURL in a deployed environment is a typical cause.
Exercise
Your Swagger UI still shows the title "OpenAPI definition" even though you wrote an OpenAPI bean with .title("Library API"). Name two likely causes.
Show answer
The configuration class may not be picked up by component scanning, for example because it sits outside the application's base package or lacks @Configuration. Another cause is that the bean is in a different group's document, or a browser cache or old build is serving stale output. Checking /v3/api-docs directly shows whether the bean is in effect.