Spring boot openapi 3.0中所有API的必填标头

Spring boot openapi 3.0中所有API的必填标头,spring-boot,swagger,swagger-ui,openapi,swagger-3.0,Spring Boot,Swagger,Swagger Ui,Openapi,Swagger 3.0,我使用的是带有Spring boot 5的OpenAPI 3.0,因此没有配置YAML。我有一个包含客户端标识ID的头(这不是身份验证头)。我想让它成为一个必需的头参数。下面添加了OpenAPI配置 @Configuration public class OpenAPIConfiguration { @Bean public OpenAPI customOpenAPI() { return new OpenAPI() .compo

我使用的是带有Spring boot 5的OpenAPI 3.0,因此没有配置YAML。我有一个包含客户端标识ID的头(这不是身份验证头)。我想让它成为一个必需的头参数。下面添加了OpenAPI配置

@Configuration
public class OpenAPIConfiguration {
    @Bean
    public OpenAPI customOpenAPI() {

        return new OpenAPI()
                .components(new Components()
                        .addParameters("myCustomHeader", new Parameter().in("header").schema(new StringSchema()).required(true).description("myCustomHeader").name("myCustomHeader")))
                .info(new Info()
                        .title("My Rest Application")
                        .version("1.2.26"));
    }
}
但是,swagger UI不会在任何API中显示所需的参数。有人能帮我解释一下我做错了什么吗


将参数定义添加到自定义的
OpenAPI
bean将不起作用,因为该参数不会传播到操作定义中。您可以通过以下方式实现您的目标:

@Bean
公共操作自定义程序自定义(){
return(operation,handlerMethod)->operation.addParametersItem(
新参数()
.in.(“标题”)
。必需(正确)
.说明(“myCustomHeader”)
.name(“myCustomHeader”);
}
springdoc openapi 1.2.22中引入了OperationCustomizer接口。在以前的版本中,您需要使用:

@组件
公共类MyOpenApicCustomizer实现OpenApicCustomizer{
私有静态最终列表操作\u GETTERS=Arrays.asList(
PathItem::getGet,PathItem::getPost,PathItem::getDelete,PathItem::getHead,
PathItem::getOptions、PathItem::getPatch、PathItem::getPut);
私有流getOperations(PathItem PathItem){
返回操作\u GETTERS.stream()
.map(getter->getter.apply(路径项))
.filter(对象::非空);
}
@凌驾
public void定制(OpenAPI){
openApi.getpath().values().stream()
.flatMap(此::getOperations)
.forEach(此::自定义);
}
私有void自定义(操作){
operation.addParametersItem(
新参数()
.in.(“标题”)
。必需(正确)
.说明(“myCustomHeader”)
.name(“myCustomHeader”);
}
}

Authentication/authorization标头应定义为。@它实际上不是一个身份验证参数,而是一个客户端标识id。没有身份验证,它是一个开放的Rest服务。更新问题。