Forum Discussion

srramasw's avatar
srramasw
New Member
3 years ago

How to specify request headers for a post method

I have a method like the following in my spring boot based web service. I am trying to generate open api spec for the following using swagger core annotations. I would like to add all the acceptable headers to the spec file. I am wondering how to do that with annotations. 

 

@POST
@Produces({ MediaType.APPLICATION_JSON + ";charset=UTF-8" })
@Operation(description = "This is my api")
//1. overall Header annotations
public
Response post(@Context HttpHeaders headers, //2. header parameters
@Context UriInfo uriInfo,
@RequestBody MyRequest myRequest) {

 

I am wondering where to put the annotations to list the headers. Also how to list all the headers. I saw many examples of headers in ApiResponse, but i am trying to add request header.

 

Thanks

Srini

1 Reply

  • Sabhtarsha's avatar
    Sabhtarsha
    New Contributor

    You can do the following in the @Operation annotation over your method

    @Operation(description = "This is my api",
        parameters = {
        @Parameter(in = ParameterIn.HEADER,
            name = "X-Version",
            description = "The API Version",
            required = true,
            schema = @Schema(type = "string"))}
    )

    You can also specify more details to the @Parameter or @Schema annotations if required.

    Checkout the last section of Operation documentation

     

    The above annotation showed up this way in my app's documentation