【问题标题】:Reusability of @ApiResponse Annotation in SwaggerSwagger 中 @ApiResponse 注解的可重用性
【发布时间】:2020-06-27 21:32:38
【问题描述】:

我正在使用 Swagger 注释在非 Spring 上下文中记录 API。 我发现 400、401 和 404 的响应文档是可重复使用的。 因为记录每个响应代码大约需要 8 行,如下所示。

    @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")
                            )
                    )
            }
    )

我打算防止每个可重用 API 响应的行变得臃肿。 我更喜欢下面的东西

  @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()
}

有没有办法在 java 8 中实现这一点?

【问题讨论】:

    标签: swagger swagger-2.0 openapi


    【解决方案1】:

    是的,在 Docket 对象上使用 globalResponseMessage。有关它们的使用示例,请参阅 springfox 文档中的 point 22。我使用这种方法从我的代码中删除了很多 @ApiResponse 注释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      • 2016-07-28
      • 2018-09-06
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      相关资源
      最近更新 更多