mak*_*aki 3 swagger spring-boot springdoc-openapi-ui
在我的 spring boot 应用程序中,我有端点,这些端点由 springboot 应用程序中的标头参数验证。当前 swagger json 如下所示:
// part of current swagger.json
...
"paths": {
"/path1/{param1}": {
"get": {
"parameters": [
{
"name": "param1",
"in": "path",
"type": "string",
"required": true
}
]
}
}
}
...
Run Code Online (Sandbox Code Playgroud)
我想使用springdoc-openapi-ui配置添加缺少的参数,所以它看起来像这样:
// I want to achieve swagger.json which contains additional parameter
...
"paths": {
"/path1/{param1}": {
"get": {
"parameters": [
{
"name": "param1",
"in": "path",
"type": "string",
"required": true
},
{
"name": "missingParam",
"in": "header",
"type": "string",
"required": true
}
]
}
}
}
...
Run Code Online (Sandbox Code Playgroud)
我尝试通过appplication.yml从各种路径的通用参数添加到我的解决方案来实现这一点
#application.yml
...
components:
parameters:
hiddenParam:
in: header
name: missingParam
required: true
schema:
type: string
paths:
/path1:
get:
parameters:
- $ref: '#/components/parameters/hiddenParam'
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
我的问题:
SSK*_*SSK 21
OperationCustomizer您可以使用如下所示添加标题等全局参数。这会将您添加parameter到每项服务中
@Configuration
public class SwaggerConfiguration {
@Bean
public OperationCustomizer customGlobalHeaders() {
return (Operation operation, HandlerMethod handlerMethod) -> {
Parameter missingParam1 = new Parameter()
.in(ParameterIn.HEADER.toString())
.schema(new StringSchema())
.name("missingParam1")
.description("header description2")
.required(true);
Parameter missingParam2 = new Parameter()
.in(ParameterIn.HEADER.toString())
.schema(new StringSchema())
.name("missingParam2")
.description("header description2")
.required(true);
operation.addParametersItem(missingParam1);
operation.addParametersItem(missingParam2);
return operation;
};
}
}
Run Code Online (Sandbox Code Playgroud)
最后我决定使用不同的方法。我定义了安全方案并将其作为授权头全局应用。
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info().title("My App").version("1.0.0"))
// Components section defines Security Scheme "mySecretHeader"
.components(new Components()
.addSecuritySchemes("mySecretHeader", new SecurityScheme()
.type(SecurityScheme.Type.APIKEY)
.in(SecurityScheme.In.HEADER)
.name("missingParam")))
// AddSecurityItem section applies created scheme globally
.addSecurityItem(new SecurityRequirement().addList("mySecretHeader"));
}
Run Code Online (Sandbox Code Playgroud)
现在swagger-ui.html允许根据测试人员的要求测试带有或不带有授权标头的端点。

干杯!
| 归档时间: |
|
| 查看次数: |
2631 次 |
| 最近记录: |