Spring + Springfox + Header Parameters

谁都会走 提交于 2019-11-28 16:46:21

问题


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

Adding a @HeaderParam method 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.


回答1:


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

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

See #22 in the documentation for more information.




回答2:


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


来源:https://stackoverflow.com/questions/36585643/spring-springfox-header-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!