package com.loan.modules.extbiz.in.rabbitmq.util; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import javax.validation.Constraint; import javax.validation.Payload; @Constraint(validatedBy = IsRMBFormatValidator.class) //具体的实现 @Target( { java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD }) @Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @Documented /** * * @description 功能描述: 自定义注解金额校验(19,4) * @param 参 数: * @Createdate 建立日期: 2016年12月28日下午2:33:38 * @Projectname 项目名称: contract-operation * @Packageclass 包及类名: com.pt.modules.contract.utils.Customannotations.IsNumberFormatValidator.java */ public @interface IsRMBberFormat { String message() default "{org.hibernate.validator.constraints.NotEmpty.message}"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
package com.loan.modules.extbiz.in.rabbitmq.util; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; /** * * @description 功能描述: 自定义注解金额校验(19,4) * @param 参 数: * @Createdate 建立日期: 2016年12月28日下午2:33:38 * @Projectname 项目名称: contract-operation * @Packageclass 包及类名: com.pt.modules.contract.utils.Customannotations.IsNumberFormatValidator.java */ public class IsRMBFormatValidator implements ConstraintValidator<IsRMBberFormat, Object>{ @Override public boolean isValid(Object IdNumber, ConstraintValidatorContext constraintValidatorContext) { String message=constraintValidatorContext.getDefaultConstraintMessageTemplate(); if(IdNumber==null||IdNumber.toString().equals("")){ constraintValidatorContext.disableDefaultConstraintViolation();//禁用默认的message的值 constraintValidatorContext .buildConstraintViolationWithTemplate(message+"不能为空").addConstraintViolation(); return false; } if(IdNumber != null){ boolean fglg = this.isRMB(IdNumber.toString()); if(fglg==true){ return true; } } constraintValidatorContext.disableDefaultConstraintViolation();//禁用默认的message的值 constraintValidatorContext .buildConstraintViolationWithTemplate(message+"只能输入数字类型长度不能大于19位").addConstraintViolation(); return false; } public boolean isRMB(String rmb){ Pattern pattern = Pattern.compile("[0-9]+(.[0-9]{1,6})");//先定义正则 Matcher isNum = pattern.matcher(rmb); if(!isNum.matches()) { return false; } if(isNum.matches()){ if(rmb.indexOf(".")!= -1){ String intNumber = rmb.substring(0,rmb.indexOf(".")); if(intNumber.length()>19){ return false; } }else{ if(rmb.length()>19){ return false; } } }else{ if(rmb.length()>19){ return false; } } return true; } @Override public void initialize(IsRMBberFormat constraintAnnotation) { // TODO Auto-generated method stub } }