java javax.validation.UnexpectedTypeException:找不到类型的验证器:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26772748/
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
javax.validation.UnexpectedTypeException: No validator could be found for type:
提问by Sreeram Gollu
the below is error we are getting when we try to put validation for size and pattern for Integer type. can you suggest, we need to set the size and pattern validation for Integer type in validator bean.xml
以下是当我们尝试对 Integer 类型的大小和模式进行验证时遇到的错误。你能建议吗,我们需要在validator bean.xml中为Integer类型设置大小和模式验证
05:58:57,342 ERROR [ErrorLoggerEJBInterceptor] Unexpected system error: No validator could be found for type: java.lang.Integer
javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.Integer
at org.hibernate.validator.engine.ConstraintTree.verifyResolveWasUnique(ConstraintTree.java:244)
回答by David Jansen
I'm just reading up on this, but could it be that type Integer cannot be applied to the Pattern constraint? It would explain why an UnexpectedTypeException is thrown, as it is only thrown if the type annoted does not match the constraints specification.
我只是在阅读此内容,但是否可以将 Integer 类型应用于 Pattern 约束?这将解释为什么抛出 UnexpectedTypeException ,因为只有在注释的类型与约束规范不匹配时才会抛出它。
I found the following for the Pattern constraint for EE7: "The annotated CharSequence must match the specified regular expression. The regular expression follows the Java regular expression conventions see Pattern. Accepts CharSequence. null elements are considered valid."
我发现 EE7 的模式约束如下:“带注释的 CharSequence 必须匹配指定的正则表达式。正则表达式遵循 Java 正则表达式约定,请参阅 Pattern。接受 CharSequence。空元素被认为是有效的。”
src: http://docs.oracle.com/javaee/7/api/javax/validation/constraints/Pattern.html
源代码:http: //docs.oracle.com/javaee/7/api/javax/validation/constraints/Pattern.html
In EE6 it seems to be limited to String: "The annotated String must... Accepts String. null elements are considered valid."
在 EE6 中,它似乎仅限于字符串:“带注释的字符串必须......接受字符串。空元素被认为是有效的。”
src: http://docs.oracle.com/javaee/6/api/javax/validation/constraints/Pattern.html
源代码:http: //docs.oracle.com/javaee/6/api/javax/validation/constraints/Pattern.html
example:
例子:
@Pattern(regexp = "<insert regex here>")
Integer evaluateMe;
This should result in an UnexpectedTypeException as the Pattern constraint expects a CharSequence (EE7) or String (EE6), but finds type Integer.
这应该会导致 UnexpectedTypeException,因为 Pattern 约束需要 CharSequence (EE7) 或 String (EE6),但发现类型为 Integer。
After typing this I read Sridhar DD's reference, which confirms it :)
输入后,我阅读了 Sridhar DD 的参考资料,这证实了这一点:)
回答by Honey Goyal
You must have implemented this class ConstraintValidator and overriding this method?
您一定已经实现了这个类 ConstraintValidator 并覆盖了这个方法吗?
@Override
public boolean isValid(XYZclass xyz, ConstraintValidatorContext context) {
Change XYZclass to Integer and implement ConstraintValidator<XYZContraint, Integer>
将 XYZclass 更改为 Integer 并实现 ConstraintValidator<XYZContraint, Integer>
回答by zhuguowei
Thanks @Honey Goyal below is my way
谢谢@Honey Goyal 下面是我的方式
@Documented
@Constraint(validatedBy = { AllowedIntegerValuesValidator.class})
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@ReportAsSingleViolation
public @interface AllowedValues {
String message() default "";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
String[] value() default { };
}
public class AllowedIntegerValuesValidator implements ConstraintValidator<AllowedValues, Integer> {
private List<Integer> valueList;
@Override
public void initialize(AllowedValues constraintAnnotation) {
valueList = new ArrayList<>();
for (String val : constraintAnnotation.value()) {
valueList.add(Integer.parseInt(val));
}
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
if (!valueList.contains(value)) {
return false;
}
return true;
}
}
@AllowedValues(value={"7","9"}, message="only could be 7 or 9")
private int location;