使用 Open API 配置的全局参数集?
Global Parameter set using Open API configurations?
我正在使用 Spring Boot REST OpenAPI 3
规范。在此示例中,我希望全局设置 headers (Custom-Header-Version=v1
),我想在向每个端点发出请求时传递它。
现在的问题是,我有 100 个 REST 端点,对于每个端点,我需要继续添加 @Parameter(in = ParameterIn.HEADER .....
这个配置,而不是我想设置它在全球范围内。有什么办法可以用OpenAPI实现吗?
有没有办法从 Spring 文档 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;
}
}
您可以试试下面的代码。 ouled saber
在上面提到的代码中添加了 .example("v1")
@Component
public class GlobalHeaderOperationCustomizer implements OperationCustomizer {
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
Parameter customHeaderVersion = new Parameter().in(ParameterIn.HEADER.toString()).name("Custom-Header-Version")
.description("Custom Header Version)").schema(new StringSchema()).example("v1").required(false);
operation.addParametersItem(customHeaderVersion);
return operation;
}
我也有同样的要求,我大摇大摆地变成了下面这样
Image from swagger ui
我正在使用 Spring Boot REST OpenAPI 3
规范。在此示例中,我希望全局设置 headers (Custom-Header-Version=v1
),我想在向每个端点发出请求时传递它。
现在的问题是,我有 100 个 REST 端点,对于每个端点,我需要继续添加 @Parameter(in = ParameterIn.HEADER .....
这个配置,而不是我想设置它在全球范围内。有什么办法可以用OpenAPI实现吗?
有没有办法从 Spring 文档 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;
}
}
您可以试试下面的代码。 ouled saber
@Component
public class GlobalHeaderOperationCustomizer implements OperationCustomizer {
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
Parameter customHeaderVersion = new Parameter().in(ParameterIn.HEADER.toString()).name("Custom-Header-Version")
.description("Custom Header Version)").schema(new StringSchema()).example("v1").required(false);
operation.addParametersItem(customHeaderVersion);
return operation;
}
我也有同样的要求,我大摇大摆地变成了下面这样
Image from swagger ui