@NotNull、@NotEmpty、@NotBlank注解使用
使用場景:如前后分離的項目,前端提交表單后,后端對必填字段進行校驗,可直接使用注解進行實現(xiàn),下面說一下這三個注解的區(qū)別。
第一:@NotNull,使用這個注解時,屬性不可以為null,但是可以為空串。
第二:@NotEmpty,使用這個注解時,屬性不可以為null,且不可以為空串(長度必須大于0)。
第三:@NotBlank,這個注解只能作用在String類型的屬性上,屬性不可以為null,且trim()后不可以為空串(長度必須大于0)。
在dto類中使用這三個屬性時,需要在對應(yīng)的controller的方法上一起使用
@valid這個注解。
String testValue =?null;
@NotNull? ? ? ??? ??false
@NotEmpty? ? ? ??false
@NotBlank? ? ? ? ??false
String testValue?=?"";
@NotNull? ? ? ??? ??true
@NotEmpty? ? ? ??false
@NotBlank? ? ? ? ??false
String testValue?=?"? ";
@NotNull? ? ? ??? ??true
@NotEmpty? ? ? ??true
@NotBlank? ? ? ? ??false
示例代碼
public?class?TestMethodDto?extends?{private String projectTitle;}
public void testMethod( TestMethodDto dto) {????????...}
評論
圖片
表情
