gpt4 book ai didi

java - springdoc-openapi 不同的例子

转载 作者:行者123 更新时间:2023-12-04 10:16:12 26 4
gpt4 key购买 nike

我使用 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
}

}

最佳答案

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

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

关于java - springdoc-openapi 不同的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61045499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com