我有自定义验证,我想在所有默认验证之后运行。我知道,当我们想要在控制器本身上验证对象但我想显式运行验证时,只需在@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 {}
.
.
. }