HV000030 No validator could be found for constraint ‘javax

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,至少包含一个非空字符串
NotEmptyCharSequence 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();
}

image-20210704215331520

正确的书写方式

    /**
     * 测试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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值