【问题标题】:How can I display error code description in sample code section?如何在示例代码部分显示错误代码描述?
【发布时间】:2020-12-13 03:11:54
【问题描述】:

我在主要部分显示了错误代码说明:

但我还想在此处的示例部分中显示这些代码描述:

我在示例部分看到了一些可以生成这些描述的 API 文档:

我该怎么做?

PS:请注意,我正在使用 springdoc 公开开放 API 规范。

【问题讨论】:

  • 你试过下面的答案吗?
  • 不幸的是,这不是我想要的,也没有成功。也许是因为我正在使用 redoc。

标签: openapi springdoc redoc


【解决方案1】:

一种方法是您可以在@ApiResponse 中定义一个字符串作为示例

您可以通过两种方式实现这一目标

1.定义您的错误消息格式类,或者您可以使用内置(如果有)

public class ExceptionResponse {

    private Instant time;
    private int status;
    private String error;
    private String exception;
    private String message;
}

然后定义您的自定义消息字符串,如下所示。

public static final String exampleInternalError = "{\r\n" + "  \"status\": 500,\r\n" + "\"error\": Bad Request,\r\n"
            + "  \"message\": \"Your custome message goes here\"\r\n" + "}";

same 用于将示例显示为

@ApiResponse(responseCode = "400", description = "Bad Request", 
                  content = @Content(schema = @Schema(implementation = ExceptionResponse .class), 
                        examples = @ExampleObject(description = "Bad Request", value = exampleInternalError)))

这将大摇大摆地显示为

2.如果你不想使用上述格式,那么你可以简单地使用

如下所述

@ApiResponse(responseCode = "400", description = "Bad Request", 
content = @Content(schema = @Schema(implementation = String.class), 
              examples = @ExampleObject(description = "Bad Request", value = "\"Your details about error code and error message goes here")))

这将大摇大摆地显示为

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-05
    • 2016-04-11
    • 2022-10-23
    • 2011-07-18
    • 1970-01-01
    • 2013-06-06
    • 2019-09-04
    • 1970-01-01
    相关资源
    最近更新 更多