问题
搭建了一个 SpringBoot 启动后报错,不科学呀,我明明是 copy 老项目的。
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint ‘org.
分析
网上查了一下,原来是我的一个实体参数类,用错了注解,一个 Integer 类型的成员变量,用了 @NotBlank。
当然报错啊!
String 类型用 @NotBlank
Integer 类型用 @NotNull
解决
去掉 @NotBlank 注解,使用 @NotNull
修改前
@NotBlank(message = "XX状态不能为空")
private Integer status;
修改后
@NotNull(message = "XX状态不能为空")
private Integer status;
知识点延伸
@NotNull://CharSequence, Collection, Map 和 Array 对象不能是 null, 但可以是空集(size = 0)。
@NotEmpty://CharSequence, Collection, Map 和 Array 对象不能是 null 并且相关对象的 size 大于 0。
@NotBlank://String 不能是 null 且去除两端空白字符后的长度(trimmed length)大于 0。