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

2
  • @HeaderParam adds a body type parameter in Swagger UI whereas the globalOperationParameters method adds a fine header type field (but is global) Commented Nov 25, 2016 at 8:37
  • For a proper header type parameter specific to one method (non global), see stackoverflow.com/questions/40801442/… Commented Nov 25, 2016 at 9:23

3 Answers 3

80

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Could you please have a look at this question - stackoverflow.com/questions/42348630/…
@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
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.
Worked for me, but I ended up creating the parameter above the Docket creation, for readability.
Great, so helpful
|
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);
    }

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.