当使用Swagger UI和带有Java Spring端点的ApiResponses注释时,如何干燥?
2022-09-03 06:58:17
我喜欢,因为它使您的api非常用户友好。我使用像这样的注释Swagger
Swagger
- @ApiParam
- @ApiResponse |@ApiResponses
- @ApiOperation
- 别人
在终结点上,查询参数、请求参数、请求正文等。
我喜欢保持我的课程干净,总的来说,我尽力遵循规则,但是,当涉及到摇摆不定时,我注意到我一遍又一遍地重复自己,如下所示POJO
DRY
@ApiOperation(value = "Retrieve object by id")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "ISE")
})
public Response retrieveById(@ApiParam(value = "Some id") @PathParam("sid") int id) {
}
@ApiOperation(value = "Create object")
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Created"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "ISE")
})
public Response create(@ApiParam(value = "Request body") RequestBody body) {
}
如何避免重复自己?Swagger annotations