@NotEmpty 用在集合类上面 @NotBlank 用在String上面 @NotNull 用在基本类型上 只有简单的结果,但是再更具体一点的内容就搜不到了,所以去看了看源码,发现了如下的注释: 1. @NotEmpty /** * Asserts that the annotated string, collection, map or array is not {@code null} or empty. * * @author Emmanuel Bernard * @author
@interface NotNull The annotated element must not be {@code null}.Accepts any type.-------------------- @interface NotEmpty * The annotated element must not be {@code null} nor empty. Supported types are:* <ul>* <li>{@code CharSequence} (lengt
1.实体类属性上添加注解规则 如 public class User { @NotBlank private Integer id ; 2.在方法中添加注解@Valid和一个校验结果参数(BindingResult类型) 如 @RequestMapping(value = "/update" ,method = RequestMethod.POST) public String update(@Valid User user ,BindingResult bindingResult){
@NotBlank注解使用时必须在入参处(controller)加@Valid. 如: public Class User{ @NotBlank(message = "用户名不能为空") private String username;} 调用时: public String getUsername(@Valid User user){} 如果不加@Valid,@NotBlank不会生效.@NotEmpty等也是一样.
1. 三者主要区别如下: @NotEmpty :用于集合类,不能为null,且size>0 @NotNull:不能为null,但可以为empty,没有size的约束 @NotBlank:只用于String,不能为null,且trim()之后size>0 2. 通过查看源码注释如下: @NotEmpty /** * Asserts that the annotated string, collection, map or array is not {@code null} or empty. *
@NotEmpty 用在集合上面(不能注释枚举) @NotBlank用在String上面 @NotNull用在所有类型上面 1. @NotEmpty Asserts that the annotated string, collection, map or array is not {@code null} or empty. 加了@NotEmpty的String类,Collection.Map.数组,是不能为null或者长度为0的(String.Collection.Map 的isEmpth(
源码解析 @NotEmpty根据JDK源码注释说明,该注解只能应用于char可读序列(可简单理解为String对象),colleaction,map,array上,因为该注解要求的是对象不为null且size>0,所以只有上述对象是拥有size属性的,而Integer,Long等基础对象包装类没有该属性 /** * The annotated element must not be {@code null} nor empty. Supported types are: * <ul> *