@NotBlank注解地正确使用

  • @NotNull:不能为null,但可以为empty
  • @NotEmpty:不能为null,而且长度必须大于0
    @NotBlank:只能作用在String上,不能为null,而且调用trim()后,长度必须大于0
    案例:
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

注意在使用@NotBlank等注解时,一定要和@valid一起使用,不然@NotBlank不起作用


img_f33041f505f51985c61134d89c0ebda3.png

img_fab1b61bdcdc8f000229abc10f04cfc1.png