我更喜欢使用@ApiImplicitParam
在我之后@RequestMapping
而不是作为函数参数,因为通常您可能会在过滤器中处理标头(例如身份验证),并且您不需要该方法中的值。
此外,如果您在方法 Swagger auto 中需要它们,则会提供以下字段:@HeaderParam
当某些调用需要标头而其他调用不需要标头时,这种样式还可以提高可读性和灵活性。
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<*>{}
如果您的端点的全部或大部分需要标头,我宁愿将其配置为所见here https://stackoverflow.com/a/36613259/3307037
如果必须声明多个标头参数,则需要使用@ApiImplicitParams
注解:
@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<*>{}