2

How to set Custom Header when using Open API 3? I am using Spring Boot + springdoc-openapi-ui example. In this example I am looking to pass the different headers in the request. Below configurations doesn't show option to select the customer header.

What else do I need to change?

@Bean
public OpenAPI customOpenAPI(@Value("${springdoc.version}") String appVersion) {
 return new OpenAPI()
      .components(new Components().addSecuritySchemes("basicScheme", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic"))
      .addParameters("myHeader1", new Parameter().in("header").schema(new StringSchema()).name("myHeader1")).addHeaders("myHeader2", new Header().description("myHeader2 header").schema(new StringSchema())))
      .info(new Info()
      .title("Petstore API")
      .version(appVersion)
      .description("This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters.")
      .termsOfService("http://swagger.io/terms/")
      .license(new License().name("Apache 2.0").url("http://springdoc.org")));
}
halfer
  • 19,824
  • 17
  • 99
  • 186
PAA
  • 1
  • 46
  • 174
  • 282

2 Answers2

8

You can add your custom Header to your operation documentation using the Following annotation @Parameter(in = ParameterIn.HEADER).

For example:

@RestController
@RequestMapping("/api")
@Tag(name = "contact", description = "the Contact API")
public class HelloController {

@Operation(summary = "Find Contacts by name", description = "Name search by %name% format", tags = {"contact"})
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "successful operation", content = @Content(array = @ArraySchema(schema = @Schema(implementation = PersonDTO.class))))})
@Parameter(in = ParameterIn.HEADER, description = "Custom Header To be Pass", name = "Accept-version"
        , content = @Content(schema = @Schema(type = "string", defaultValue = "v1", allowableValues = {"v1", "v2"}, implementation = PersonDTO.class)))
@GetMapping(value = "/contacts", /*produces = { "application/json", "application/xml" },*/ headers = {"Accept-version=v10"})
public ResponseEntity<List<PersonDTO>> findAll(
        @Parameter(description = "Page number, default is 1") @RequestParam(value = "page", defaultValue = "1") int pageNumber,
        @Parameter(description = "Name of the contact for search.") @RequestParam(required = false) String name) {

    return null;
}}
  • Could you pls guide me here: https://stackoverflow.com/questions/60853683/spring-docs-open-api-3-how-to-set-default-values-to-body – PAA Apr 03 '20 at 10:19
  • 2
    This worked for me but that means I have to add @Parameters to each request in all controllers. How to do it for all request at once? – pixel Jun 23 '22 at 21:00
0

SOLVED: To have header parameters applied to all requests:

All the suggestions I found didn't work and did some digging and found this solution below:

@Bean
public GlobalOpenApiCustomizer customizer() {
    return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream())
                              .forEach(operation -> operation.addParametersItem(new HeaderParameter().name("[YOUR_NAMR]")
                                                                                        .description("[YOUR_DESC]")
                                                                                        .in(ParameterIn.HEADER.toString())
                                                                                        .schema(new Schema<>().format("[e.g UUID]").type("[e.g STRING]")) //will be: string($uuid)
                                                                                        .required(true)));
}

Found the solution here: https://springdoc.org/faq.html

NXT Dev
  • 71
  • 4