问题
How to validate the length of elements inside List using javax.validation.constraints in Spring. Right now @Size is validating on the list size, not on the inside elements.
class RequestInputParamaters {
@NotNull
@NotEmpty
@Size(min = 1, max=4)
List documentIdentifier_value
}
回答1:
Try:
List<@NotNull @NotEmpty @Size(min = 1, max=4) String> documentIdentifier_value;
If using hibernate-validator
, you'll need version 6+.
Legacy solution:
@Valid List<StringWrapper> documentIdentifier_value;
where StringWrapper
is defined as:
public class StringWrapper {
@NotNull @NotEmpty @Size(min = 1, max=4)
private String wrapped;
...
}
来源:https://stackoverflow.com/questions/49939444/how-to-validate-the-length-of-elements-inside-list-using-javax-validation-constr