Define common swagger annotation

China☆狼群 提交于 2019-12-24 09:48:45

问题


Admittedly, I didn't look too hard for an answer. My question is, when defining swagger annotations using springfox-swagger-ui, how does one define common ApiResponse used for more then one method and potentially more then one class?

See the 2 sample methods below the common ApiResponse is error 500. Ideally, I would want to define that once. Any best practices and/or suggestions?

Sample code below:

@GET
@Path("/greeting")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Returns greeting details", notes = "Returns .....", response = Greeting.class)
@ApiResponses(value = {
    @ApiResponse(code = 200, message = "Successful retrieval of greeting details", response = Greeting.class),
    @ApiResponse(code = 404, message = "Greeting does not exist"),
    @ApiResponse(code = 500, message = "Internal server error")}
)
public Response getGreeting(.....) {
    ...
}


@GET
@Path("/something")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Returns something details", notes = "Returns .....", response = Something.class
@ApiResponses(value = {
    @ApiResponse(code = 200, message = "Successful retrieval of greeting details", response = Something.class),
    @ApiResponse(code = 404, message = "Something does not exist"),
    @ApiResponse(code = 500, message = "Internal server error")}
)
public Response getSomething(.....) {
    ...
}

Specifically referring to:

@ApiResponse(code = 500, message = "Internal server error")

Thank you, in advance.


回答1:


you can create custom annotation which use ApiResponses annotation. And use this custom annotation at method level. @ApiResponses() public @interface CustomApiResponse {}




回答2:


Since @ApiResponse is an annotation, you need to add it for every method you want including it on @ApiResponses annotation.



来源:https://stackoverflow.com/questions/44682260/define-common-swagger-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!