spring - 无法在 Open API UI 中显示自定义标题
问题描述
使用 Open API 3 时如何设置自定义标题?我正在使用 Spring Boot + springdoc-openapi-ui 示例。在此示例中,我希望在请求中传递不同的标头。以下配置不显示选择客户标题的选项。
我还需要改变什么?
@Bean
public OpenAPI customOpenAPI(@Value("${springdoc.version}") String appVersion) {
return new OpenAPI()
.components(new Components().addSecuritySchemes("basicScheme", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic"))
.addParameters("myHeader1", new Parameter().in("header").schema(new StringSchema()).name("myHeader1")).addHeaders("myHeader2", new Header().description("myHeader2 header").schema(new StringSchema())))
.info(new Info()
.title("Petstore API")
.version(appVersion)
.description("This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.")
.termsOfService("http://swagger.io/terms/")
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
}
解决方案
您可以使用以下注释 @Parameter(in = ParameterIn.HEADER) 将自定义 Header 添加到操作文档中。
例如:
@RestController
@RequestMapping("/api")
@Tag(name = "contact", description = "the Contact API")
public class HelloController {
@Operation(summary = "Find Contacts by name", description = "Name search by %name% format", tags = {"contact"})
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "successful operation", content = @Content(array = @ArraySchema(schema = @Schema(implementation = PersonDTO.class))))})
@Parameter(in = ParameterIn.HEADER, description = "Custom Header To be Pass", name = "Accept-version"
, content = @Content(schema = @Schema(type = "string", defaultValue = "v1", allowableValues = {"v1", "v2"}, implementation = PersonDTO.class)))
@GetMapping(value = "/contacts", /*produces = { "application/json", "application/xml" },*/ headers = {"Accept-version=v10"})
public ResponseEntity<List<PersonDTO>> findAll(
@Parameter(description = "Page number, default is 1") @RequestParam(value = "page", defaultValue = "1") int pageNumber,
@Parameter(description = "Name of the contact for search.") @RequestParam(required = false) String name) {
return null;
}}
推荐阅读
- javascript - ajax调用未达到成功功能
- c# - 用于 C# 中 Rabbit MQ 模拟连接的 xUnit
- unity3d - 在 Unity 中模拟深度相机
- python - 将可调用/绑定方法变成对人类更易读的东西?
- python - 将 json 对象列表展平为表,其中包含 Databricks 中每个对象的列
- html - 悬停时将一个块移到另一个块上
- python - Python - 在数据框中运行 for 循环的更快方法
- c# - 在基类中定义后,是否需要在派生类中定义相同的接口?
- javascript - 将一些数据结果迁移到另一个jsp页面
- sql - sql - 多表选择最后一个值名称