@Valid:仅显示错误消息中的几个字段

时间:2021-01-06 22:08:42

标签: java spring spring-boot validation

我正在开发 API 并使用 @Valid 来验证传入的请求。一切正常,但最终的错误响应包含许多我不想要的字段。有什么办法可以自定义这个,并且只显示几个字段?

{
    "timestamp": "2021-01-06T21:18:25.908+0000",
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.web.bind.MethodArgumentNotValidException",
    "errors": [
        {
            "codes": [
                "NotBlank.text",
                "NotBlank.text",
                "NotBlank.text",
                "NotBlank.java.lang.String",
                "NotBlank"
            ],
            "arguments": [
                {
                    "codes": [
                        "text",
                        "text"
                    ],
                    "arguments": null,
                    "defaultMessage": "text",
                    "code": "text"
                }
            ],
            "defaultMessage": "code cannot be not null or empty",
            "objectName": "request",
            "field": "text",
            "rejectedValue": "",
            "bindingFailure": false,
            "code": "NotBlank"
        }
    ],
    "message": "Validation failed for object='request'. Error count: 1",
    "path": "/somepath"
}

0 个答案:

没有答案