使用Open API配置设置全局参数? [英] Global Parameter set using Open API configurations?

查看:459
本文介绍了使用Open API配置设置全局参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot REST OpenAPI 3规范.在此示例中,我希望全局设置向每个端点发出请求时要传递的标头(Custom-Header-Version=v1).

I am using Spring Boot REST OpenAPI 3 specification. In this example, I am looking to globally set the headers (Custom-Header-Version=v1) which I want to pass while making a request to each endpoint(s).

现在的问题是,我有 100个REST端点,对于每个端点,我需要继续添加@Parameter(in = ParameterIn.HEADER .....,此配置,而我一直在寻找全局设置.有什么办法可以在OpenAPI中做到?

Now issue is that I've 100 of REST endpoint and for each endpoint I need to keep adding @Parameter(in = ParameterIn.HEADER ....., this configuration, instead I was looking to set it globally. Is there any way if we can do it in OpenAPI?

是否可以从Spring doc ui中删除SmartBear徽标?

Is there any way to remove SmartBear logo from Spring doc ui ?

@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;
        }
}

推荐答案

,您可以尝试以下代码. ouled saber

you can try the following code. Added .example("v1") in the code mentioned above by 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;
    }

我有相同的要求,大摇大摆,我变得像下面

I have same requirement and on swagger I am getting like below

来自招摇的ui的图像

这篇关于使用Open API配置设置全局参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆