java - springdoc-openapi 不同的例子

标签 java springdoc

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

相关文章:

java - 将 Angular js 与 taglib 结合使用

java - Spring Doc swagger API 上的日期格式时间

java springdoc @ApiResponses 如何定义一个列表作为返回对象使用

java - Twitter 趋势 API - org.json.JSONException (Android)

java - Kafka Mirror Maker 执行地点

spring-boot - Spring Boot 3 和 OpenApi 3 (1.6.13) 不起作用。白标错误页面

swagger-ui - 在SpringDoc OpenAPi中未禁用petstore URL

spring-boot - springdoc-openapi 如何显示字符串数组作为响应

java - 如何在 Android 中为文件创建隐式 Intent

java - 没有数据库的 Hibernate/JPA