我正在使用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中有什么方法可以实现这一目标吗?
答案 0 :(得分:0)
是的,在globalResponseMessage
对象上使用Docket
。有关其用法的示例,请参见springfox文档上的point 22。我使用这种方法从代码中删除了许多@ApiResponse
注释。