是否可以重写spring hibernate验证器的默认响应pojo?
当前,当验证失败时,一个非常大的响应返回给客户端,如下所示。但是我不希望客户机提供hibernate验证器的完整错误响应,而是发送一些关于错误消息的键值对。
{
"timestamp": "2018-05-28T18:12:56.705+0000",
"status": 400,
"error": "Bad Request",
"errors": [
{
"codes": [
"NotBlank.abc.xyz",
"NotBlank.xyz",
"NotBlank.java.lang.String",
"NotBlank"
],
"arguments": [
{
"codes": [
"abc.xyz",
"xyz"
],
"arguments": null,
"defaultMessage": "transactionId",
"code": "transactionId"
}
],
"defaultMessage": "xyz is mandatory parameter , please provide appropriate value",
"objectName": "abc",
"field": "xyz",
"rejectedValue": "",
"bindingFailure": false,
"code": "NotBlank"
}
],
"message": "Validation failed for object='xyz'. Error count: 1",
"path": "/path/create/1"
}
1条答案
按热度按时间dgsult0t1#
一
BindException
当请求主体验证失败时抛出。你可以自己定义ControllerAdvice
从中的详细信息构造适当的错误消息BindException
.错误响应的pojo:
错误消息示例