Add a header parameter in Swagger UI documentation with Springfox

后端 未结 3 1279
梦毁少年i
梦毁少年i 2020-12-19 02:26

I want to add a header parameter field in the auto-generated swagger ui documentation of my rest service. I use Spring and Springfox.

public ResponseEntity&l         


        
3条回答
  •  时光取名叫无心
    2020-12-19 02:42

    I prefer to use @ApiImplicitParam after my @RequestMapping rather than as function parameters because generally you might process your headers in a filter (eg authentication) and you are not needing the values in that method.

    Besides if you need them in the method Swagger auto provides the field for a @HeaderParam

    This style also Improves readability and flexibility when some calls need headers and other don't.

    Example

    @PostMapping
    @ApiImplicitParam(name = "Authorization", value = "Access Token", required = true, allowEmptyValue = false, paramType = "header", dataTypeClass = String::class, example = "Bearer access_token")
    fun addJob(jobRequest: Job): ResponseEntity<*>{}
    

    If all or most for your endpoints need header that I'll rather configure it as seen here

    If you have to declare several header params, you need to use the @ApiImplicitParams annotation:

    @PostMapping
    @ApiImplicitParams(
      @ApiImplicitParam(name = "Authorization", value = "Access Token", required = true, allowEmptyValue = false, paramType = "header", dataTypeClass = String::class, example = "Bearer access_token"),
      @ApiImplicitParam(name = "X-Custom-Header", value = "A Custom Header", required = true, allowEmptyValue = false, paramType = "header", dataTypeClass = String::class, example = "my header example")
    )
    fun addJob(jobRequest: Job): ResponseEntity<*>{}
    

提交回复
热议问题