在何处指定用于bean验证的组顺序

时间:2018-06-29 12:29:37

标签: java javax.validation

我有自定义验证,我想在所有默认验证之后运行。我知道,当我们想要在控制器本身上验证对象但我想显式运行验证时,只需在@Validated批注中指定组序列即可。

Set<ConstraintViolation<Listing>> violations = Validation.buildDefaultValidatorFactory().getValidator().validate(listing);
    if (CollectionUtils.isNotEmpty(violations))
        violations.stream().forEach(violation -> errorList.add(violation.getMessage()));

我无法弄清楚如何指定我们要使用此组序列(ListingConstraintGroupSequence.class)。

@ListingAdditionalPropertiesConstraint(groups = 
        ListingCustomValidationGroup.class,
        additionalProperties = "additionalProperties"
)
@ListingInventoryLocationsConstraint(groups = ListingCustomValidationGroup.class,
        listingInventoryLocations = "listingInventoryLocations"
)
@ListingConditionalFieldsConstraint.List({
        @ListingConditionalFieldsConstraint(groups = ListingCustomValidationGroup.class,
                message = "maxLimitPerUser cannot be 0 when limited is true",
                booleanFlag = "limited",
                field = "maxLimitPerUser"
        ),
        @ListingConditionalFieldsConstraint(groups = ListingCustomValidationGroup.class,
                message = "maxDaystoReturn cannot be 0 when willAcceptReturns is true",
                booleanFlag = "willAcceptReturns",
                field = "maxDaystoReturn"
        )
})
@EqualsAndHashCode
@Getter
@Setter
@ToString
public class Listing implements Serializable {

    //Ensures that Custom Validators are run only if the default validations pass
    @GroupSequence({Default.class,ListingCustomValidationGroup.class})
    public interface ListingConstraintGroupSequence {}
 .
 .
 . }

引荐的问题:Bean validation group sequence not working

0 个答案:

没有答案