簡體   English   中英

springdoc-openapi 不同的例子

[英]springdoc-openapi different examples

我使用 springdoc-openapi 來記錄我的 REST API。 錯誤 object 返回錯誤,該錯誤具有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
    }

}

一種方法是您可以定義一個字符串作為示例

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

同樣用於將示例顯示為

@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)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM