HV000030: No validator could be found for constraint ‘javax.validation.constraints.NotEmpty’ validating type ‘java.lang.Integer’. Check configuration for ‘serviceType’
情景在线
最近项目中参数校验实体类有个tag标签属性,存放的是类型的id,需要做非空校验,我使用的注解是@NotEmpty,但是报错
我自己百度了下,一开始花了十分钟都没有解决,后来经过多次网上查找资料,找到了原因
参数校验非空复习
参考网址:
https://blog.csdn.net/lazy_cat_go/article/details/110638384?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162540639316780269850586%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=162540639316780269850586&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-1-110638384.pc_search_result_cache&utm_term=+HV000030%3A+No+validator+could+be+found+for+constraint+%27javax.validation.constraints.NotEmpty%27+validating+type+%27java.lang.Integer%27.+Check+configuration+for+%27serviceType%27&spm=1018.2226.3001.4187
Null | 任意类型 | 参数值必须是 Null |
---|---|---|
NotNull | 任意类型 | 参数值必须不是 Null |
NotBlank | 只能作用于字符串 | 字符串不能为 null,而且字符串长度必须大于0,至少包含一个非空字符串 |
NotEmpty | CharSequence Collection Map Array | 字符串:字符串长度必须大于0,空字符串(“ ”)可以通过校验; Collection:集合大小必须大于0; Map:map的大小必须大于0; Array:数组长度必须大于0; |
重点说明
对于非空校验我们建议使用@NotBlank,因为字符窜需要符合非空字符窜,防止前端传参数为""
复现报错情况
springboot测试的controller方法
/**
* 测试Integer类型得伟数据判空
*/
@ApiOperation(value = "测试Integer类型判空")
@ApiImplicitParam(name = "id",value = "Integer类型的id,测试Integer类型的非空校验",example = "1")
@GetMapping("/testValitorIntegr")
public Result testValitorIntegr(
@Validated @NotBlank(message = "Integer类型的id不能为空")
@RequestParam(value = "id",required = true) Integer id) {
System.out.println("----------------------");
return Result.ok();
}
正确的书写方式
/**
* 测试Integer类型得伟数据判空
*/
@ApiOperation(value = "测试Integer类型判空")
@ApiImplicitParam(name = "id",value = "Integer类型的id,测试Integer类型的非空校验",example = "1")
@GetMapping("/testValitorIntegr")
public Result testValitorIntegr(
@Validated @NotNull(message = "Integer类型的id不能为空")
@RequestParam(value = "id",required = true) Integer id) {
System.out.println("----------------------");
return Result.ok();
}
把@NotBlank改为@NotNull
总结
1.字符窜非空校验使用
@NotBlank
2.数值包装类的非空校验使用
@NotNull
3.集合的非空校验使用
@NotEmpty
空校验使用
@NotBlank
2.数值包装类的非空校验使用
@NotNull
3.集合的非空校验使用
@NotEmpty