【发布时间】:2020-06-27 21:32:38
【问题描述】:
我正在使用 Swagger 注释在非 Spring 上下文中记录 API。 我发现 400、401 和 404 的响应文档是可重复使用的。 因为记录每个响应代码大约需要 8 行,如下所示。
@Operation(
summary = "getDetails",
description = "someDescription",
responses = {
@ApiResponse(
responseCode = "200",
description = "Found",
content = @Content(mediaType = "application/json",
schema = @Schema(
name = "Response for success")
)
),
@ApiResponse(
responseCode = "400",
description = "Validation failure on inputs.",
content = @Content(
mediaType = "application/json",
schema = @Schema(
name = "Response For Bad Request")
)
),
@ApiResponse(
responseCode = "404",
description = "Not found",
content = @Content(
mediaType = "application/json",
schema = @Schema(
name = "Response For Not Found Request")
)
),
@ApiResponse(
responseCode = "401",
description = "Unauthorized",
content = @Content(
mediaType = "application/json",
schema = @Schema(
name = "Response For Unauthorized")
)
)
}
)
我打算防止每个可重用 API 响应的行变得臃肿。 我更喜欢下面的东西
@Operation(
summary = "getDetails",
description = "someDescription",
responses = {
@ApiResponse(
responseCode = "200",
description = "Found",
content = @Content(mediaType = "application/json",
schema = @Schema(
name = "Response for success")
)
),
SomeStaticClass.getBadResponseDesc(),
SomeStaticClass.getUnauthorizedResponseDesc()
}
有没有办法在 java 8 中实现这一点?
【问题讨论】:
标签: swagger swagger-2.0 openapi