找不到约束“javax.validation.constraints.Size”的验证器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50160778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:28:19  来源:igfitidea点击:

No validator could be found for constraint 'javax.validation.constraints.Size'

javaspringhibernatebean-validation

提问by Lucas. D

I have the following error

我有以下错误

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Size' validating type 'java.lang.Integer'. Check configuration for 'discounted'

javax.validation.UnexpectedTypeException:HV000030:找不到约束“javax.validation.constraints.Size”验证类型“java.lang.Integer”的验证器。检查“折扣”的配置

My code is:

我的代码是:

@Column(name= "discount_percentage")
@Size(min=0, max=90)
@Min(0)
@Max(90)
private Integer discountPercentage = 0;

I set it to 0 because i was getting a NullPointerException when loading my view. And is Integer because i was reading in others question, and some people says that sometimes there are problems when using @Size with primitive types.

我将它设置为 0,因为我在加载视图时收到 NullPointerException。并且是整数,因为我正在阅读其他问题,有些人说有时将@Size 与原始类型一起使用时会出现问题。

What should i do? Thanks in advance.

我该怎么办?提前致谢。

回答by M?nh Quy?t Nguy?n

@Sizeis not used to validate min/max.

@Size不用于验证最小值/最大值。

It's used to validate size of collections, length of strings, etc.

它用于验证集合的大小、字符串的长度等。

In this case you should use @Min, @Maxinstead.

在这种情况下,您应该使用@Min,@Max代替。

Refer here for complete document: https://docs.oracle.com/javaee/7/api/javax/validation/constraints/Size.html

请参阅此处获取完整文档:https: //docs.oracle.com/javaee/7/api/javax/validation/constraints/Size.html

回答by Amit Bera

@Sizeis a Bean Validation annotation that validates that the associated Stringhas a value whose lengthis bounded by the minimum and maximumvalues. And as your exceptionsays it does not apply to Integertype.

@Size是一个 Bean Validation 注释,用于验证关联String的值length是否受这些minimum and maximum值限制。正如您exception所说,它不适用于Integer类型。

Use: @Range

用: @Range

@Column(name= "discount_percentage")
@Range(min=0, max=90)
private Integer discountPercentage = 0;

Or you cloud also use only @Max or @Minand that will work too. For more info please take a look on thislink.

或者你也只使用云@Max or @Min,那也行。有关更多信息,请查看链接。