2

SpringBoot 应用程序在出现未处理错误时的默认服务器响应是

{
    "timestamp": 1594232435849,
    "path": "/my/path",
    "status": 500,
    "error": "Internal Server Error",
    "message": "this request is failed because of ...",
    "requestId": "ba5058f3-4"
}

我想在应用程序路由的 Springdoc 注释中描述它。

假设有一个标准类DefaultErrorResponse(只是一个模拟名称),它可能如下所示:

@Operation(
  // ... other details
  responses = {
    // ... other possible responses
    @ApiResponse(
      responseCode = "500",
      content = @Content(schema = @Schema(implementation = DefaultErrorResponse.class)))
  }
)

在更糟糕的情况下,这样的类不存在,Spring 仅Map在后台使用 a 来创建此响应。然后这个注释会更详细,包括明确提及响应中包含的每个字段。

显然,对于大多数路线,这部分@ApiResponse(responseCode="500",...是相同的,减少重复会很好。

在文档中引入默认错误响应描述的正确方法是什么?

4

1 回答 1

2

对于错误处理,您将@RestControllerAdvice 与@ExceptionHandler 结合使用,以重构错误处理。

这些 spring 注释由 springdoc-openapi 自动扫描。无需添加任何额外的招摇注解。

于 2020-07-11T09:58:08.923 回答