Java 如何在@ApiModelProperty中设置示例值,以便为HTTP状态代码返回不同的值?

Java 如何在@ApiModelProperty中设置示例值,以便为HTTP状态代码返回不同的值?,java,spring-boot,swagger,swagger-ui,springfox,Java,Spring Boot,Swagger,Swagger Ui,Springfox,我有一个名为ErrorDetails的类,它是所有错误响应的模板 public class ErrorDetails { private Date timestamp; private String message; private String details; private int code; public ErrorDetails(Date timestamp, String message, String details, int code) { this

我有一个名为
ErrorDetails
的类,它是所有错误响应的模板

public class ErrorDetails {
  private Date timestamp;
  private String message;
  private String details;
  private int code;

  public ErrorDetails(Date timestamp, String message, String details, int code) {
    this.timestamp = timestamp;
    this.message = message;
    this.details = details;
    this.code = code;
  }
}
现在,为了显示示例值,我们使用了swagger注释:
@ApiModel
&
@ApiModelProperty
。但是我想为各自的错误响应显示不同的示例。 例如,
code 400
应具有示例消息:
未找到页面
,对于
code 500
将具有不同的示例消息。我如何做到这一点?对于特定情况,我只能指定一个示例值。有没有一种方法可以通过编程方式处理示例值?我探讨了以下方法:

  • 在全局摘要配置中设置通用响应: 这里的问题是,仅代码404可能会有不同的错误响应。例如:“找不到员工”、“找不到商店”等。如果我有全局配置,这是不可能实现的
  • 使用
    @Example
    @ApiResponse
    : 这里的问题是这不起作用&springfox开发团队建议使用
    @ApiModelProperty
    而不是这种方法
  • @ApiResponse(code=500,message=“Internal server error”,response=ErrorDetails.class,examples=@ExampleProperty(value=”{“timestamp\”:\“1598947603319\,“message\”:“操作失败”,“详细信息\”:“由于运行时异常导致操作失败”,“code\”:“500\”,mediaType=“application/json”))

    上述代码不起作用,我得到以下输出:


    有人能建议我如何解决上述问题吗?或者如何在
    @ApiModelProperty
    中动态设置值?

    如果您只需要显示附加到哪个状态代码的消息,下面是示例

        @PostMapping(value = "/api/posts")
        @ApiOperation(value = "Create post.")
        @ApiResponses({
            @ApiResponse(code = 200, message = "Post created successfully", response = PostResponse.class),
            @ApiResponse(code = 404, message = "Blog does not exist", response = ApiError.class),
            @ApiResponse(code = 409, message = "Post already exists", response = ApiError.class),
            @ApiResponse(code = 422, message = "Block content full", response = ApiError.class)
    })
    PostResponse createPost(...){
      ...
    }
    

    显然,我必须创建不同的类来表示这些错误中的每一个,并从
    @ApiResponse
    链接它们。我不可能有一个模型类可以为所有不同的HTTP状态抛出不同的样本响应