Swagger 和 Springfox 更改不同状态代码的示例响应

Swagger and Springfox Changing the example response for different status codes

我有一个特定的 DTO return 作为 Swagger 文档的示例。如果我有成功的 201 代码,我该如何更改示例?

大摇大摆的回应示例:

我使用注释“@ApiModelProperty”来描述我的对象,但找不到如何更改不同响应代码示例的方法。在我的代码中,我不想显示错误列表,因为它是一个可选的 属性 并且只有在生成非 201 代码时才可用。

想法?

您可以使用如下几个注释来完成此操作:

import io.swagger.v3.oas.annotations.media.ExampleObject;

@Operation(summary = "Your summary")
@ApiResponses(value = { 
  @ApiResponse(responseCode = "200", description = "Your description", 
    content = { @Content(mediaType = "application/json", 
      schema = @Schema(implementation = YourModel.class),
      examples = [@ExampleObject(value = "{\"timestamp\": 1581552186590, \"status\": 404, \"error\": \"Not Found\", \"message\": \"Error message\", \"requestId\": \"62bcf95d\"}")]) })})