Adding @NotNull or Pattern constraints on List

前端 未结 4 1956
失恋的感觉
失恋的感觉 2020-12-20 16:00

How can we ensure the individual strings inside a list are not null/blank or follow a specific pattern

@NotNull
List emailIds;
4条回答
  •  甜味超标
    2020-12-20 16:24

    You can create a simple wrapper class for the e-mail String:

    public class EmailAddress {
    
        @Pattern("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b.")
        String email;
    
        //getters and setters
    }
    

    Then mark the field @Valid in your existing object:

    @NotNull
    @Valid
    List emailIds;
    

    The validator will then validate each object in the list.

提交回复
热议问题