我应该抛出什么异常来返回与使用@valid时得到的错误请求响应相同的错误请求响应?

8gsdolmq  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(133)

我正在通过 SpringValidatorAdapter 如下例所示:

Set<ConstraintViolation<CertificationDto>> violations = validator.validate(certificationDto, ValidationGroups.Update.class);
if (!violations.isEmpty()) {
    // FIXME Wrong exception, doesn't appear as usual validation response
    throw new ConstraintViolationException(violations);
}

我希望保持错误响应的异构性,并正在寻找以下内容:

{
    "timestamp": "2021-03-20T18:28:57.343+00:00",
    "status": 400,
    "error": "Bad Request",
    "message": "Validation failed for object='certificationDto'. Error count: 1",
    "errors": [
        {
            "codes": [
                "NotBlank.certificationDto.name",
                "NotBlank.name",
                "NotBlank.java.lang.String",
                "NotBlank"
            ],
            "arguments": [
                {
                    "codes": [
                        "certificationDto.name",
                        "name"
                    ],
                    "arguments": null,
                    "defaultMessage": "name",
                    "code": "name"
                }
            ],
            "defaultMessage": "must not be blank",
            "objectName": "certificationDto",
            "field": "name",
            "rejectedValue": null,
            "bindingFailure": false,
            "code": "NotBlank"
        }
    ],
    "path": "/certifications"
}

要抛出的正确异常是什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题