0

我通过 a 手动验证 bean 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"
}

抛出的正确异常是什么?

4

0 回答 0