No validator could be found for constraint ‘javax.validation.constraints.Pattern‘ validating type ‘j
原文链接: https://blog.csdn.net/wangxuelei036/article/details/107079783
今天使用validation中的 @Pattern 突然报错,提示 pattern 没有对应的校验器去验证Integer类型参数,什么鬼?
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Pattern' validating type 'java.lang.Integer'. Check configuration for 'age'
看看代码
这里想通过正则去验证以下年龄是否在我们自定义的规则里面
通过各种尝试和查阅终于得到答案
原来是 项目中使用的校验注解所支持的数据类型与实体中字段的类型不符合。意思就是该类型上面无法使用这个注解。
例:在Integer类型的字段上使用@NotEmpty,@Pattern 都不行 因为支持的是字符串类型字段,这样子使用肯定是会报错的。
那么我该怎么办呢?
解决方法:
(1)换个校验注解,或者不使用校验注解
例: 将Integer类型的字段上使用的@NotEmpty @Pattern 换成@NotNull,然后再在代码中进行校验
(2)将Integer 类型转为 String 类型,如果允许的话,又或者单独冗余一个String字段用于校验,一个字段用于业务
(3)直接放开校验,在代码层去进行保证
(4)自定义一个校验器进行处理
注:@Pattern 只能校验有值时候正则是否满足,不能校验空的问题,如果传入的参数为空,默认是正确的
/**
* @author Hardy Ferentschik
*/
public class PatternValidator implements ConstraintValidator<Pattern, CharSequence> {
........
public boolean isValid(CharSequence value, ConstraintValidatorContext constraintValidatorContext) {
if ( value == null ) {
return true;
}
Matcher m = pattern.matcher( value );
return m.matches();
}
}
如果你想要判断不能为空,同时满足正则可以进行组合注解验证
:
相关文章
- 【Vue】错误提示Parsing error: No Babel config file detected for 的解决办法
- TP:DD4FAE06 Thanks for TexturePacker
- 解决:No appenders could be found for logger
- SSH整合报错:No result defined for action and result input
- struts2报错:There is no Action mapped for namespace [/] and action name [userAction!add]
- 解决spring-boot配置文件使用加密方式保存敏感数据启动报错No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly
- 解决 IDEA 下载源码失败的问题maven-default-http-blocker (http://0.0.0.0/) Blocked mirror for repositories
- Mingyang.net:No identifier specified for entity
- No toolchains found in the NDK toolchains folder for ABI with prefix
- listen - listen for connections on a socket 在一个套接字上倾听连接
- Troubleshooting Guide for ORA-12541 TNS: No Listener
- Linux-004-解决 Tomcat 启动时提示 Insufficient space for shared memory file
- eclipse卡死在search for main types 20 files to index
- ArcGIS for Server 的修改IP问题
- mysql创建表时,设置timestamp DEFAULT NULL报错1067 - Invalid default value for 'updated_at'
- 源码分析 There is no getter for property named '*' in 'class java.lang.String
- Mac软件安装篇 for Mac
- How to select a OptionSet on ms sqlserver database for Microsoft Dynamics CRM
- Configure the AD FS server for claims-based authentication -zhai zi wangluo
- 警告 - no rule to process file 'WRP_CollectionView/README.md' of type net.daringfireball.markdown for architecture i386
- IIS 报错 Cannot open database "test4" requested by the login. The login failed. Login failed for user 'IIS APPPOOLtest1'.
- No FileSystem for scheme “hdfs“