我正在尝试对该字段进行约束验证,如果输入不是整数,则必须拒绝。
public class ClientTO {
private Integer phone_num; //
}
Run Code Online (Sandbox Code Playgroud)
我试过:
1) @Digits
- 它不验证输入是否为整数,因为您可以看到仍然抛出类型不匹配异常。
2)我的自定义验证 - 这似乎不适用于Integer
字段
错误:
Exception in thread "main" javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.
Run Code Online (Sandbox Code Playgroud)
我的自定义验证类:
public class X_CVAImpl implements ConstraintValidator<X_CustomValidatorAnnotation,String>{
public boolean isValid(String value, ConstraintValidatorContext context) {
// TODO Auto-generated method stub
boolean val_d;
if (value.matches("[0-9]+") && value.length() ==10) {
val_d=true;
}else {
val_d=false;
}
return val_d;
}
}
Run Code Online (Sandbox Code Playgroud)
请问有什么帮助吗?
我如何创建datetime
,int
与当时的初始化变量null
,我尝试这一点,但它显示的错误
[datetime] $var1 = $null
Run Code Online (Sandbox Code Playgroud)
错误:
WARNING: Error: Cannot convert value "var1" to type "System.DateTime". Error: "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."
Run Code Online (Sandbox Code Playgroud)