首页 > 解决方案 > 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?

标签: swaggerswagger-2.0openapi

解决方案


是的,globalResponseMessageDocket物体上使用。有关它们的使用示例,请参见springfox 文档中的第 22 点。我使用这种方法从我的代码中删除了很多@ApiResponse注释。


推荐阅读