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.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
也就是说,加了@NotEmpty的String类、Collection、Map、数组,是不能为null或者长度为0的。(String、Collection、Map的isEmpty()方法)

@NotNull

/**
* The annotated element must not be {@code null}.
* Accepts any type.
*
* @author Emmanuel Bernard
*/
即不能为null。

@NotBlank

/**
* Validate that the annotated string is not {@code null} or empty.
* The difference to {@code NotEmpty} is that trailing whitespaces are getting ignored.
*
* @author Hardy Ferentschik
*/

和{@code NotEmpty}不同的是,尾部空格被忽略,也就是说,纯空格的String也是不符合规则的。所以才会说@NotBlank用于String。

3. 示例:

1.String name = null;
@NotNull: false
@NotEmpty:false
@NotBlank:false 2.String name = "";
@NotNull:true
@NotEmpty: false
@NotBlank: false 3.String name = " ";
@NotNull: true
@NotEmpty: true
@NotBlank: false 4.String name = "Great answer!";
@NotNull: true
@NotEmpty:true
@NotBlank:true
 

@NotEmpty、@NotNull 和 @NotBlank 的区别的更多相关文章

  1. spring boot -表单校验步骤 +@NotEmpty,@NotNull和@NotBlank的区别

    1.实体类属性上添加注解规则 如 public class User { @NotBlank private Integer id ; 2.在方法中添加注解@Valid和一个校验结果参数(Bindin ...

  2. @NotEmpty@NotNull和@NotBlank的区别

    这几个可以为对象,不只是字符串 1.@NotNull 不能为null,但可以为empty (""," "," ") 2.@NotEmpty ...

  3. @NotEmpty、@NotNull、@NotBlank 的区别

    @NotEmpty 用在集合上面(不能注释枚举) @NotBlank用在String上面 @NotNull用在所有类型上面 1. @NotEmpty Asserts that the annotate ...

  4. @NotNull、@NotEmpty、@NotBlank的区别

    Spring中@NotNull.@NotEmpty.@NotBlank的区别@NotNull:用于基本数据类型@NotEmpty:用于集合类@NotBlank:用于String上面

  5. 开发中常见的@NotNull,@NotBlank,@NotEmpty注解的区别

    开发中常看见@NotNull,@NotBlank,@NotEmpty三个注解,但却没有深入了解过,下面介绍一下他们的应用场景和区别 @NotNull:主要用在基本数据类型上(Int,Integer,D ...

  6. @NotNull 、@NotBlank、@NotEmpty区别

    @NotNull: 主要用在基本数据类型上(Integer.Double...) 不能为null,但可以为empty 举例: @NotNull(message = "标题不能为空" ...

  7. @NotNull丶@NotBlank丶@NotEmpty

    1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...

  8. @NotEmpty、@NotNull、@NotBlank注解解析

    源码解析 @NotEmpty根据JDK源码注释说明,该注解只能应用于char可读序列(可简单理解为String对象),colleaction,map,array上,因为该注解要求的是对象不为null且 ...

  9. springboot注解@NotNull,@NotBlank,@Valid自动判定空值

    一.前言 搭建springboot项目,我们都是采用的Restful接口,那么问题来了,当前端调用接口或者是其他项目调用时,我们不能单一靠调用方来控制参数的准确性,自己也要对一些非空的值进行判定. 二 ...

随机推荐

  1. Spring jdbcTemplat 写入BLOB数据为空

        近日做平台新闻接口,数据库用的是Oracle10g,项目使用Spring框架,新闻表内有一字段为BLOB类型,可是在写入时遇到了写入后BLOB字段为空,替换了Spring 的jar包无效,跟b ...

  2. Mysql中与时间相关的统计分析

    最近项目需要统计一段日期范围内,根据每分钟.几分钟.每天分别统计汇总某些事件/指标的发生总次数,平均发生次数,因此总结了Mysql中与时间处理.统计相关的资料. 按分钟统计某一时间段内的数据 SELE ...

  3. 终端控制类getopt isatty select ttyname

    getopt(分析命令行参数) 相关函数 表头文件 #include<unistd.h> 定义函数 int getopt(int argc,char * const argv[ ],con ...

  4. Spark向HDFS中存储数据

    程序如下: import org.apache.spark.sql.Row; import org.apache.spark.SparkConf; import org.apache.spark.ap ...

  5. 网站日志访问记录组件UserVisitLogsHelp开源了!

    之前在<一种基于自定义代码记录用户访问日志在Sharepoint网站的应用方法!>一文利用本人几年前的开发的UserVisitLogsHelp组件进行了网站用户访问日志记录,可用于网站分析 ...

  6. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  7. Android -- ImageLoader简析

    图片的内存缓存实现 Image-Loader库有一个较完整的内存缓存实现,使用者可以根据需要选择已经实现的策略,也可以定制自己项目中需要的策略. 内存缓存实现代码在memory和memory.impl ...

  8. Linq-语句之存储过程

    存储过程 在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中怎么使用呢?也许比原来的更简单些.下面我们以NORTHWND.MDF数据库中自带的几个存储过程来理解一下. 1.标量返回 在 ...

  9. Angular报错

    报错: Module 'App' is not available! You either misspelled the module name or forgot to load it. If re ...

  10. mybatis的statement的解析与加载(springboot)

    问题 mybatis的xml中的sql语句是启动时生成JDK代理类的时候就生成一次么 调用顺序链 解析xml配置 Reader reader = Resources.getResourceAsRead ...