Я использую springdo c -openapi для документирования своего REST API. Ошибка возвращается объектом ошибки, который имеет errorCode
и message
. Я использую аннотацию @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
}
}