自定义 OpenAPI 规范
添加元数据、服务器和安全方案
自定义 OpenAPI 规范 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Microservices & REST APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Customizing the whole spec
Beyond per-endpoint annotations, you can shape the entire document - title, version, contact, license, servers and security - by defining an OpenAPI bean.
Defining an OpenAPI bean
Return an OpenAPI object from a @Bean method and SpringDoc uses it as the base document.
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("Orders API")
.version("v1")
.description("Public ordering service"));
}The info section
The Info object holds metadata shown at the top of Swagger UI: title, version, description, terms of service, contact and license.
new Info()
.title("Orders API")
.version("1.2.0")
.contact(new Contact().name("API Team").email("api@acme.com"))
.license(new License().name("Apache 2.0").url("https://apache.org/licenses/LICENSE-2.0"));Declaring servers
List the base URLs where the API is reachable. Swagger UI lets users pick a server, and generated clients use these as base paths.
new OpenAPI()
.addServersItem(new Server().url("https://api.acme.com").description("Production"))
.addServersItem(new Server().url("http://localhost:8080").description("Local"));Why servers matter behind a proxy
When your app sits behind a gateway or context path, the auto-detected server URL can be wrong. Declaring servers explicitly ensures "Try it out" calls hit the correct address.
Security schemes
Document how clients authenticate by registering a SecurityScheme in the components, e.g. a bearer JWT.
new OpenAPI()
.components(new Components()
.addSecuritySchemes("bearerAuth",
new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")));Applying security globally
Add a SecurityRequirement so the UI shows a lock icon and lets users supply a token for all secured operations.
new OpenAPI()
.addSecurityItem(new SecurityRequirement().addList("bearerAuth"))
.components(/* scheme defined above */);Per-operation security
To secure only some endpoints, use @SecurityRequirement on the controller method instead of declaring it globally.
@SecurityRequirement(name = "bearerAuth")
@GetMapping("/admin/stats")
public Stats stats() { ... }Grouping APIs with GroupedOpenApi
For large apps you can split docs into named groups (e.g. public vs admin) using GroupedOpenApi, each with its own path matchers.
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("public")
.pathsToMatch("/public/**")
.build();
}Customizing via properties
Some settings can be done without code through springdoc.* properties - sorting operations, packages to scan, and which paths to include.
# application.yml
springdoc:
packages-to-scan: com.acme.api
paths-to-match: /api/**
swagger-ui:
operations-sorter: methodOpenApiCustomizer for fine control
For programmatic tweaks to every operation, implement an OpenApiCustomizer bean and mutate the document after generation - e.g. add a common header to all paths.
Quick Check
Test your spec-customization understanding.
Recap
You customized the whole document:
- An
OpenAPIbean setsInfo, servers and security - Declare servers for proxies/context paths
SecurityScheme+SecurityRequirementdocument authGroupedOpenApisplits large APIs
常见问题解答
「自定义 OpenAPI 规范」课时是免费的吗?
是的 — 「自定义 OpenAPI 规范」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
「自定义 OpenAPI 规范」这节课中我会学到什么?
添加元数据、服务器和安全方案 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Boot 4 Microservices & REST APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Boot 4 Microservices & REST APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「自定义 OpenAPI 规范」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Boot 4 Microservices & REST APIs 课中编写并运行代码吗?
能。每节 Spring Boot 4 Microservices & REST APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 将 SpringDoc 添加到项目
- 记录端点和模型
- 自定义 OpenAPI 规范
- Swagger UI