Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring + Springfox + Header Parameters

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

like image 361
Jay Anderson Avatar asked Apr 12 '16 23:04

Jay Anderson


People also ask

Does Springfox support OpenAPI 3?

Recently, the popular Springfox project released the long-awaited v3 of their library with support for OpenAPI 3 and Spring 5 (only annotation-based API is supported).

What is Springfox Swagger2?

Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser. To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file.

How does Springfox work?

Springfox works by examining an application, once, at runtime to infer API semantics based on spring configurations, class structure and various compile time java Annotations.


2 Answers

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.

like image 68
Dilip Krishnan Avatar answered Sep 21 '22 08:09

Dilip Krishnan


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);     } 
like image 36
Vijay Avatar answered Sep 24 '22 08:09

Vijay