import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@NotNull:多用在基本数据类型上(Int,Integer,Double)
举例:
@ApiModelProperty("年龄")
@NotNull(message = "年龄不能为空!")
private Integer age;
@NotBlank:多用在String字符串上面(String)
举例:
@ApiModelProperty("用户代码")
@NotBlank(message = "用户代码不能为空!")
private String userCode;
@NotEmpty; 加了@NotEmpty注解的String类 ,Collection集合,Map ,数组,这些是不能为null或者长度为0的;(String ,Collection,Map的isEmpty()方法)
@ApiModelProperty("学生")
@NotEmpty(message = "至少有一个学生!")
private List<String> stdId;
主要判断对象:
1.@NotNull
源码解释:
The annotated element must not be {@code null}.
带此注解的元素不能是null。
源码解释:
Asserts that the annotated string, collection, map or array is not {@code null} or empty.
断言带此注解的字符串、集合、映射或数组不是null或为空。
即,不为null,不为空!如在List上则该列表的size不为0。
源码解释:
Validate that the annotated string is not {@code null} or empty.
The difference to {@code NotEmpty} is that trailing whitespaces are getting ignored.
断言带此注解的字符串、集合、映射或数组不是null或为空,与@NotEmpty注解不同的是末尾的空格会被忽略!
即,不为null,不为空,不为空格!
说明:
1、上述的空(empty) 为 没有该属性即前端不传该属性!
三者断言的范围大小排序:
@NotNull < @NotEmpty < @NotBlank
2、若要使上述的@NotNull , @NotEmpty , @NotBlank注解生效,
需要在使用到的实体类前加上@Valid注解,如下:
@RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
@ApiOperation(value = "新增", notes = "新增")
public AddParam add(@RequestBody @Valid AddParam addParam) {...}