How to set example values in @ApiModelProperty in such a way that different values are returned for HTTP status codes?

huangapple go评论71阅读模式
英文:

How to set example values in @ApiModelProperty in such a way that different values are returned for HTTP status codes?

问题

I have a class called ErrorDetails which is a template for all error responses.

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;
    }
}

Now, in order to display example values, we use swagger annotations: @ApiModel & @ApiModelProperty. But I want to display different samples for respective error responses.
For example, code 400, should have a sample message: Page not found, and for code 500 will have a different sample message. How do I achieve that? I can only specify one example value for a specific case. Is there a way to programmatically handle sample values? I have explored the below ways:

  1. Having a generic response set at global docket config:
    Problem here is I might have a different error response for code 404 alone. For example: "Employee not found", "Store not found" etc. This is impossible to achieve if I have global configuration.

  2. Using @Example with @ApiResponse:
    Problem here is this is not working, and springfox dev team suggests using @ApiModelProperty instead of this approach.

 @ApiResponse(code = 500, message = "Internal server error", response = ErrorDetails.class,
                     examples = @Example(value = @ExampleProperty(value =
                             "{\"timestamp\": \"1598947603319\", \"message\":\"Operation failed\", \"details\":\"Operation failed due to a Runtime exception\", \"code\": \"500\"}", mediaType = "application/json")))

The above code is not working, and I get the below output:
How to set example values in @ApiModelProperty in such a way that different values are returned for HTTP status codes?

Can someone suggest me how to fix the above issue? or how to set values dynamically in @ApiModelProperty?

英文:

I have a class called ErrorDetails which is a template for all error responses.

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;
  }
}

Now, in order to display example values, we use swagger annotations: @ApiModel & @ApiModelProperty . But I want to display different samples for respective error responses.
For example, code 400, should have sample message: Page not found and for code 500 will ahve different sample message. How do I achieve that? I can only specify one example value for a specific case. Is there a way to programmatically handle sample values? I have explored below ways:

  1. Having a generic response set at global docket config:
    Problem here is I might have different error response for code 404 alone. For example : "Employee not found", "Store not found" etc. This is impossible to achieve if I have global configuration.
  2. Using @Example with @ApiResponse:
    Problem here is this is not working & springfox dev team suggest to use @ApiModelProperty instead of this approach.

@ApiResponse(code = 500, message = "Internal server error", response = ErrorDetails.class,
examples = @Example(value = @ExampleProperty(value =
"{\"timestamp\": \"1598947603319\", \"message\":\"Operation failed\", \"details\":\"Operation failed due to Runtime exception\", \"code\": \"500\"}", mediaType = "application/json")))

The above code is not working and I get the below output:
How to set example values in @ApiModelProperty in such a way that different values are returned for HTTP status codes?

Can someone suggest me how to fix the above issue ? or how to set values dynamically in @ApiModelProperty ?

答案1

得分: 0

如果您只需要显示附加到哪个状态代码的消息,那么这是一个示例:

@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(...){
  ...
}
英文:

If you need to only display what message is attached to which status code, then here's example

    @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(...){
  ...
}

答案2

得分: 0

显然,我必须创建不同的类来代表这些错误,并从 @ApiResponse 中链接它们。没有办法使用单个模型类为所有不同的HTTP状态抛出不同的示例响应。

英文:

Apparently, I had to create different classes representing each one of these errors and link them from @ApiResponse. There's no way that I can have one single model class that can throw varying sample responses for all different HTTP status.

huangapple
  • 本文由 发表于 2020年9月11日 20:20:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63846986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定