36
@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.

Jay Anderson
  • 937
  • 1
  • 8
  • 18
  • @HeaderParam adds a body type parameter in Swagger UI whereas the globalOperationParameters method adds a fine header type field (but is global) – Julien Nov 25 '16 at 08:37
  • For a proper header type parameter specific to one method (non global), see http://stackoverflow.com/questions/40801442/add-a-header-parameter-in-swagger-ui-documentation-with-springfox/40801443 – Julien Nov 25 '16 at 09:23

3 Answers3

78

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.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
Dilip Krishnan
  • 5,417
  • 3
  • 37
  • 53
  • Could you please have a look at this question - http://stackoverflow.com/questions/42348630/customizing-request-header-description-in-swagger-ui-using-springfox-swagger2 – Gandhi Feb 21 '17 at 04:42
  • @Dilip I am getting a compile error "The method newArrayList(Parameter) is undefined for the type SwaggerConfiguration" while trying to use this snippet. I am using Swagger version 2.8.0. Aything I am missing? Could you pls suggest – user09 May 15 '18 at 20:20
  • That snippet is defined in a bean definition method as shown . Perhaps you're not placing the snippet correctly. Also hopefully you're removing the `...` etc. – Dilip Krishnan May 16 '18 at 11:40
  • 1
    Worked for me, but I ended up creating the parameter above the Docket creation, for readability. – yngwietiger Aug 02 '18 at 20:06
  • Great, so helpful – Wafula Samuel Jul 25 '19 at 15:03
  • Can we add cookie as a header parameter @DilipKrishnan? – Sanket Patel Nov 13 '19 at 06:32
  • more on this blog https://dev.to/s2agrahari/global-header-in-swagger-ui-spring-boot-5188 – Suraj May 23 '20 at 12:45
19

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);
    }
Vijay
  • 4,694
  • 1
  • 30
  • 38
  • 1
    Thank you for the answer it helped a lot, change last return statement to return new Docket(DocumentationType.SWAGGER_2).select() .apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build().pathMapping("").globalOperationParameters(aParameters); – Nazeel Dec 20 '17 at 00:20
1

globalOperationParameters is depressed, use RequestParameter and globalRequestParameters with springfox-boot-starter:3.0.0

    @Bean
    public Docket api() {
        List<RequestParameter> aParameters = new ArrayList<>();

        RequestParameterBuilder aParameterBuilder = new RequestParameterBuilder();
        aParameterBuilder.name("appId").in(ParameterType.HEADER).required(true);
        aParameters.add(aParameterBuilder.build());
        aParameterBuilder.name("appSecret");
        aParameters.add(aParameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any()).paths(PathSelectors.ant("/api/*")).build().pathMapping("")
                .globalRequestParameters(aParameters);
    }
user2204107
  • 111
  • 1
  • 4