【问题标题】:springdoc-openapi different examplesspringdoc-openapi 不同的例子
【发布时间】:2020-07-17 14:34:13
【问题描述】:

我使用 springdoc-openapi 来记录我的 REST API。错误对象返回错误,该对象具有errorCodemessage。我使用@Schema 注释来记录一个示例。但是,对于不同的错误,我需要不同的示例。有什么办法吗,怎么办?

我的代码示例:

    @PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    @Operation(summary = "Get new license or retrieve previously issued one for this userId.", tags = "License Endpoint", description = "Licensing operations.",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "New license or previously issued license for this user, if request was called multiple times.",
                            content = {@Content(schema = @Schema(implementation = LicenseResponse.class))}
                    ),
                    @ApiResponse(responseCode = "400",
                            description = "License can not be retrieved because of either expired bundle or requested bundleId does not exist.",
                            //I need different example for this error
                            content = {@Content(schema = @Schema(implementation = LicenseErrorResponse.class))
                            }
                    ),
                    @ApiResponse(responseCode = "500",
                            description = "Internal Error",
                            //And different example for this error
                            content = {@Content(schema = @Schema(implementation = LicenseErrorResponse.class))
                            }
                    )
            }
    )
    @LoggedIO(input = INFO, result = INFO)
    public ResponseEntity<Object> newLicense(@Valid @RequestBody LicenseRequest licenseRequest) {
//content not interesting
}

import javax.validation.constraints.NotBlank;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
public class LicenseErrorResponse {

    // I need different examples for different error in controller.
    @Schema(example = "UNKNOWN_BUNDLE_ID", required = true)
    private final LicenseErrorCode licenseErrorCode;

    @Schema(example = "Bundle doesn't exist, bundleId=com.unknown.id")
    private final String message;

    @JsonCreator
    public LicenseErrorResponse(
                @NotBlank @JsonProperty(value = "errorCode") final LicenseErrorCode licenseErrorCode,
                @NotBlank @JsonProperty(value = "message") final String message) {
        this.licenseErrorCode = licenseErrorCode;
        this.message = message;
    }

    public enum LicenseErrorCode {
        EXPIRED_BUNDLE, UNKNOWN_BUNDLE_ID, OTHER
    }

}

【问题讨论】:

    标签: java springdoc


    【解决方案1】:

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

    public static final String exampleInternalError = "{\r\n"
                + "  \"licenseErrorCode\": 500,\r\n"
                + "  \"message\": \"Internal Error\"\r\n" + "}";
    

    same 用于将示例显示为

    @ApiResponse(responseCode = "500",
                                description = "Internal Error",
                                //And different example for this error
                                content = @Content(schema = @Schema(implementation = LicenseErrorResponse.class), 
                                       examples = @ExampleObject(description = "Internal Error", value = exampleInternalError)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-02
      • 2021-11-09
      • 1970-01-01
      • 1970-01-01
      • 2022-12-08
      • 2022-12-06
      • 1970-01-01
      • 2020-07-23
      相关资源
      最近更新 更多