Swagger:为特定@Api路径下的所有请求定义通用参数

时间:2019-06-17 05:59:24

标签: java spring-mvc swagger springfox

我正在尝试重构某些代码,并为特定的@Api路径(每个控制器一次)全局定义一个常用的HeaderParameter(现在将其称为“ myCommonParam”)。否则,我必须为控制器中的每个操作/ http请求定义HeaderParameter。

进行一些研究之后,我发现了一种通过springfox配置(示例1)指定globalOperationParameters的方法。

示例1:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                ...
                .globalOperationParameters(Collections.singletonList(
                new ParameterBuilder()
                        .name("myCommonParam")
                        .description("my common param.")
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .required(false)
                        .build())
        );
    }
}

这种方法的实际问题是我有多个控制器(api路径),并且只希望HeaderParameter应用于其中的一些。最好的解决方案是,如我期望的那样,我每个班级可以定义一次常用的参数。

实际:

@Api(value = "firstController")
public class MyFirstController {

    public void apiOperation_1(@RequestHeader(name = "myCommonParam", required = false) @ApiParam(value = "my common param.", required = false) String myCommonParam){
        // not neccessary
    }

    public void apiOperation_2(@RequestHeader(name = "myCommonParam", required = false) @ApiParam(value = "my common param.", required = false) String myCommonParam){
        // not neccessary
    }
}

预期:

@Api(value = "firstController")
>>> somehow define the 'myCommonParam' parameter here? <<<
public class MyFirstController {


    public void apiOperation_1(){
        // not neccessary
    }

    public void apiOperation_2(){
        // not neccessary
    }
}

0 个答案:

没有答案