如何在示例代码部分显示错误代码说明?

问题描述

我在主要部分显示错误代码描述:

image

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

image

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

image

我该怎么做?

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

解决方法

一种实现方法是,您可以在@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" + "}";

相同用于将示例显示为

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

这将大摇大摆地显示为 enter image description here

2。。如果您不想以上述格式使用,则只需使用

如下所述

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

这将大摇大摆地显示为 enter image description here