问题
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