【发布时间】:2020-06-04 15:33:20
【问题描述】:
我正在验证传入的 POST 请求,该请求将在验证请求数据后创建一个数据库实体。我正在尝试在单个请求中收集多个错误,并按照 JSON API 规范作为错误响应进行响应:
https://jsonapi.org/examples/#error-objects-multiple-errors
HTTP/1.1 400 Bad Request
Content-Type: application/vnd.api+json
{
"errors": [
{
"status": "403",
"source": { "pointer": "/data/attributes/secretPowers" },
"detail": "Editing secret powers is not authorized on Sundays."
},
{
"status": "422",
"source": { "pointer": "/data/attributes/volume" },
"detail": "Volume does not, in fact, go to 11."
},
{
"status": "500",
"source": { "pointer": "/data/attributes/reputation" },
"title": "The backend responded with an error",
"detail": "Reputation service not responding after three requests."
}
]
}
@ControllerAdvice 可以做到这一点吗?当@ControllerAdvice启用全局异常处理并抛出异常时,不会捕获下一个异常。
【问题讨论】:
标签: spring spring-boot error-handling json-api controller-advice