英文:
SpringBoot-OpenAPI: How to add default header to all requests
问题
以下是翻译好的内容:
现在,我想要在所有请求中添加一个额外的头部MyCustomHeader: MyValue
,该如何实现?
英文:
I have created an OpenAPI spec as below.
@Bean
public OpenAPI customOpenAPI() {
final String securitySchemeName = "Authorization";
final String apiTitle = "My Service";
return new OpenAPI()
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
.components(
new Components()
.addSecuritySchemes(securitySchemeName,
new SecurityScheme()
.name(securitySchemeName)
.type(SecurityScheme.Type.APIKEY)
.in(SecurityScheme.In.HEADER)))
.info(new Info().title(apiTitle));
}
Now, I want OpenAPI UI to add one extra header MyCustomHeader:MyValue
to all the requests. How can I achieve this?
答案1
得分: 1
你可以按照以下方式定义OpenApiCustomiser bean:
@Bean
public OpenApiCustomiser openApiCustomizer() {
return openApi -> openApi.getPaths().values().stream()
.flatMap(pathItem -> pathItem.readOperations().stream())
.forEach(
operation -> operation.addParametersItem(
new HeaderParameter()
.schema(new StringSchema()._default("MyValue"))
.name("MyCustomHeader")));
}
这个bean将MyCustomHeader
头部添加到所有以MyValue
为默认值的端点上。
英文:
You can define OpenApiCustomiser bean as follows:
@Bean
public OpenApiCustomiser openApiCustomizer() {
return openApi ->
openApi.getPaths().values().stream()
.flatMap(pathItem -> pathItem.readOperations().stream())
.forEach(
operation ->
operation.addParametersItem(
new HeaderParameter()
.schema(new StringSchema()._default("MyValue"))
.name("MyCustomHeader")));
}
The bean adds the MyCustomHeader
header to all endpoints with MyValue
as default value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论