2

我有以下来自第三方 jar 的 java 外部类,我想对这个类进行 bean 验证。

产品.java:

private String id;
private Price price

价格.java

private String price1;
private String price2;

下面是相同的休眠验证器:

private Validator createDataValidator() {
    HibernateValidatorConfiguration hibernateValidatorConfiguration = Validation
        .byProvider(HibernateValidator.class)
        .configure();
    ConstraintMapping constraintMapping =
            hibernateValidatorConfiguration.createConstraintMapping();
    constraintMapping
        .type(Product.class)
        .field("id")
        .constraint(new NotNullDef().message("id should not be null"))

        .type(Price.class)
        .field("price1")
        .constraint(new NotNullDef().message("price1 should not be null"))

        .field("price2")
        .constraint(new NotNullDef().message("price2 should not be null"))

    Validator validator =
            hibernateValidatorConfiguration.addMapping(constraintMapping)
                .buildValidatorFactory()
                .getValidator();
    return validator;
}

对于 Price 的嵌套类,验证没有按预期进行,非常感谢您的帮助。

4

0 回答 0