swagger - Reusability of @ApiResponse Annotation in Swagger
问题描述
I am using Swagger annotations to document API in non-spring context. I find that response documentation for 400, 401 and 404 is reusable. Since it takes around 8 lines to document each response code as shown below.
@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")
)
)
}
)
I intend to prevent bloating the lines for each reusable API response. I would prefer something like below
@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()
}
Is there any way to achieve this in java 8?
解决方案
是的,globalResponseMessage
在Docket
物体上使用。有关它们的使用示例,请参见springfox 文档中的第 22 点。我使用这种方法从我的代码中删除了很多@ApiResponse
注释。
推荐阅读
- algorithm - 如何创建一个带有循环的链表?
- sqlite - 每 3 行 Sqlite 总和
- javascript - 反应如何使用样式化组件更改按钮的背景
- javascript - 如何在 Angular 11 中使用 aws-iot-device-sdk NPM 模块?(它在构建时抛出 fs、path 和 tls 错误)
- python - Python - os.walk(在文件资源管理器中搜索文件)对我不起作用
- arrays - 在 C99 中的连续内存中分配两个全局数组
- typescript - 是否可以从 docker-compose 运行命令 npm test?
- wiremock - 使用wiremock时异步测试的ConditionTimeout
- spring-boot - 无法将“server.port”下的属性绑定到 java.lang.Integer
- google-apps-script - 由于错误 401,无法再运行脚本:deleted_client OAuth 客户端已删除