我正在使用Spring Boot REST OpenAPI 3
规范。在此示例中,我希望全局设置向每个端点发出请求时要传递的标头(Custom-Header-Version=v1
)。
现在的问题是,我已经REST端点的100,并且需要为每个端点继续添加@Parameter(in = ParameterIn.HEADER .....
和此配置,而我希望在全局进行设置。有什么办法可以在OpenAPI中完成?
有什么方法可以从Spring doc ui中删除SmartBear徽标?
@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"}, implementation = PersonDTO.class)))
@GetMapping(value = "/contacts", headers = {"Custom-Header-Version=v1"})
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;
}
}
您只能定义一个OperationCustomizer。
@Component
public class GlobalHeaderOperationCustomizer implements OperationCustomizer {
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
Parameter parameterHeader = new Parameter()
.in(ParameterIn.HEADER.toString())
.schema(new StringSchema().addEnumItem("v1")._default("v1").name("Accept-version"))
.description("Custom Header To be Pass");;
operation.addParametersItem(parameterHeader);
return operation;
}
}