How to validate the length of elements inside List using javax.validation.constraints in Spring

南笙酒味 提交于 2020-01-05 06:59:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!