package cc.zeelan.framework.utils.valid.custom; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import javax.validation.Constraint; import javax.validation.Payload; /** * 自定义ID验证规则 * @author witts * @project core-utils * @package cc.zeelan.framework.valid.custom * @version 1.0 * @message 林花谢了春红,太匆匆。无奈朝来寒雨,晚来风 */ @Documented @Constraint(validatedBy = {_IDCustomValid.class }) @Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface IDCustomValid { String message() default "ID为32位有效ID"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
package cc.zeelan.framework.utils.valid.custom; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; /** * ID注解规则设置 * @author witts * @project seller-core-utils * @package cc.zeelan.framework.utils.valid.custom * @version 1.0 * @message 林花谢了春红,太匆匆。无奈朝来寒雨,晚来风 */ public class _IDCustomValid implements ConstraintValidator<IDCustomValid,String>{ @Override public void initialize(IDCustomValid constraintAnnotation) {} @Override public boolean isValid(String value, ConstraintValidatorContext context) { if(null == value) { return false; } if(value.trim().length()>=32) { return true; } return false; } }