1. 程式人生 > >@NotNull和@NotEmpty和@NotBlank區別

@NotNull和@NotEmpty和@NotBlank區別

  • 1.@NotNull:不能為null,但可以為empty

    (""," ","   ")      
    
  • 2.@NotEmpty:不能為null,而且長度必須大於0

    (" ","  ")
    
  • 3.@NotBlank:只能作用在String上,不能為null,而且呼叫trim()後,長度必須大於0

    ("test")    即:必須有實際字元
    

*

        @NotNull: The CharSequence, Collection, Map or Array object is not null, 
        but can be empty.

        @NotEmpty: The CharSequence, Collection, Map or Array object is not null 
        and size > 0.

        @NotBlank: The string is not null and the trimmed length is greater than
         zero.
  • 4.examples:

    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
    

*參考連結: