我正在使用 springdoc 库生成一个 OpenAPI 定义,我想描述在某些服务深处抛出的默认异常的响应类型。
特别是当抛出 ResponseStatusException
异常并生成此 JSON 时,会返回标准错误响应:
{
"timestamp": "2021-02-23T17:56:20.523+00:00",
"status": 400,
"error": "Bad Request",
"message": "The user provides contradictory organization information",
"path": "/api/vehicles"
}
我还没有找到实现这些字段的单个类,并怀疑这些字段只是一个一个地添加到响应中。
更复杂的错误响应是验证响应。我也没有找到描述这些错误的类:
{
"timestamp": "2021-02-23T17:58:09.662+00:00",
"status": 400,
"error": "Bad Request",
"message": "Validation failed for object='addVehicleDto'. Error count: 1",
"errors": [
{
"codes": [
"Pattern.addVehicleDto.licensePlate",
"Pattern.licensePlate",
"Pattern.java.lang.String",
"Pattern"
],
"arguments": [
{
"codes": [
"addVehicleDto.licensePlate",
"licensePlate"
],
"arguments": null,
"defaultMessage": "licensePlate",
"code": "licensePlate"
},
[],
{
"defaultMessage": "^[A-ZÖÜÄ]{1,3} [A-ZÖÜÄ]{1,2} [1-9][0-9]{0,3}[E]?$",
"arguments": null,
"codes": [
"^[A-ZÖÜÄ]{1,3} [A-ZÖÜÄ]{1,2} [1-9][0-9]{0,3}[E]?$"
]
}
],
"defaultMessage": "must match \"^[A-ZÖÜÄ]{1,3} [A-ZÖÜÄ]{1,2} [1-9][0-9]{0,3}[E]?$\"",
"objectName": "addVehicleDto",
"field": "licensePlate",
"rejectedValue": "1",
"bindingFailure": false,
"code": "Pattern"
}
],
"path": "/api/vehicles"
}
我自己模拟了“标准”错误响应并添加了以下注释,但我不确定我可以使用什么 implementation
来处理验证错误。对于 400 个请求,我可能会用 Scheme.oneOf
添加。
@PostMapping
@ResponseStatus(ACCEPTED)
@Operation(summary = "Add vehicle",
responses = {
@ApiResponse(responseCode = "400", description = "The user provides contradictory organization information",
content = @Content(schema = @Schema(implementation = ResponseStatusExceptionResponse.class))),
@ApiResponse(responseCode = "403", description = "The user is not allowed to add vehicles")}
)
public CompletableFuture<UUID> add