Java annotation for Null but neither Empty nor Blank

后端 未结 5 681
别那么骄傲
别那么骄傲 2020-12-13 06:38

Is there are any java annotation(s) that can validate like the example below?

String test;
test = null; //valid
test = ""; //invalid
test = " &         


        
5条回答
  •  不知归路
    2020-12-13 06:56

    Where is a nice javax.validation.constraints.Pattern annotation.

    You can annotate the field with:

    @Pattern(regexp = "^(?!\\s*$).+", message = "must not be blank")
    

    This checks if field matches regex. The regex itself is something but not blank (see details here). It uses negative lookahead.

提交回复
热议问题