Spring + Springfox + Header 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36585643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:02:55  来源:igfitidea点击:

Spring + Springfox + Header Parameters

springswagger-uispringfox

提问by Jay Anderson

@RequestMapping(...)
public Foo getFoo(@HeaderParam("header") final String header) {
    ...
}

Adding a @HeaderParammethod parameter as above springfox picks it up and when I look at the swagger-ui it has a field for the header. This is exactly what I want. Is there a way I can tell springfox to include this header parameter on a set of methods without having to include the parameters on the method itself? What we really have going on is a servlet filter which uses the header and we'd like an easy to set it through the swagger-ui.

添加一个@HeaderParam方法参数,如上 springfox 拿起它,当我查看 swagger-ui 时,它有一个标题字段。这正是我想要的。有没有一种方法可以告诉 springfox 在一组方法中包含这个头参数,而不必在方法本身中包含参数?我们真正要做的是使用标头的 servlet 过滤器,我们希望通过 swagger-ui 轻松设置它。

回答by Dilip Krishnan

You could use the globalOperationParametersin the docket definition. For e.g.

您可以globalOperationParameters在文档定义中使用 。例如

new Docket(...)
            .globalOperationParameters(
        Arrays.asList(new ParameterBuilder()
            .name("header")
            .description("Description of header")
            .modelRef(new ModelRef("string"))
            .parameterType("header")
            .required(true)
            .build()))

See #22in the documentation for more information.

有关更多信息,请参阅文档中的#22

回答by Opster Elasticsearch Pro-Vijay

One more explained answer for same :-

另一个解释相同的答案:-

@Bean
    public Docket api() {
        //Adding Header
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder.name("headerName").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
        List<Parameter> aParameters = new ArrayList<Parameter>();
        aParameters.add(aParameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build().apiInfo(apiInfo()).pathMapping("").globalOperationParameters(aParameters);
    }