在使用validator时,有个需求就是公用错误提示信息,什么意思?
举个例子:
@NotEmpty非空判断,在资源文件中我不想每个非空判断都写”不能为空“,只需要写”###“,然后提示信息自动会变成”###不能为空“
代码:
public class User{ //资源文件中user.name.empty=用户名
@NotEmpty(key={user.name.empty}) private String name; '''}//加入name为空,则最终的错误提示为“用户名不能为空”(会自动加上“不能为空”信息)2、实现方式
有两种实现方式
方式一:手动调用验证方法注解@Target({FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@ReportAsSingleViolation
@Constraint(validatedBy = {})
@NotNull
@Size(min = 1)public @interface NotEmpty {
String message() default "{key}{com.chyjr.hyb.validator.constraints.empty.message}";
Class>[] groups() default { };
Class extends Payload>[] payload() default { };
String key() default "";
}验证器//验证器
public class MyValidator {
private static final Logger log = LoggerFactory.getLogger(HybValidator.class);
private static Validator validator = null; private static MessageInterpolator msgInterpolator = null;
static { if (validator == null) {
LocalValidatorFactoryBean factory =
(LocalValidatorFactoryBean) ApplicationContextUtil.getBean("validator");
validator = factory.getValidator();
msgInterpolator = factory.getMessageInterpolator();
}
} public static HybValidatorResult validate(Object object, Class>... groups) {
HybValidatorResult result = new HybValidatorResult();
Set> violations = validator.validate(object, groups);
Map map = new HashMap<>(); if (CollectionUtils.isEmpty(violations)) {
result.setErrors(false);
} else {
result.setErrors(true); for (ConstraintViolation 使用//调用验证方法获得验证结果
HybValidatorResult bvr = HybValidator.validate(emp, Createvalidator.class); //表示有错误
if (bvr.isErrors()) {
}
//资源文件内容//my.empty.message=不能为空//user.name.empty=用户名方式二:用spring自带的@Validated,无需调用验证方法这里有个问题:@Validated注解不认注解@NotEmpty中的key,如何解决呢?
最终的实现方案:自定义验证器
代码:
注解@documented
@Target({FIELD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@ReportAsSingleViolation//指定验证器@Constraint(validatedBy = NotEmptyValidator.class)public @interface NotEmpty {
String message() default "{my.empty.message}";
Class>[] groups() default { };
Class extends Payload>[] payload() default { };
String key() default "";
}验证器:自定义public class NotEmptyValidator extends AbstractValidator使用:{ @Override public void initialize(NotEmpty notEmpty) { } @Override public boolean doIsValid(Object value, ConstraintValidatorContext cc) { return value != null; } }public abstract class AbstractValidator implements ConstraintValidator{ @Override public abstract void initialize(A constraintAnnotation); @Override public boolean isValid(T value, ConstraintValidatorContext context){ //获取验证结果,采用模板方法 boolean result = doIsValid(value,context); //当验证错误时修改默认信息 if(!result){ //改变默认提示信息 if(ConstraintValidatorContextImpl.class.isAssignableFrom(context.getClass())){ ConstraintValidatorContextImpl constraintValidatorContext = (ConstraintValidatorContextImpl)context; //获取默认提示信息 String defaultConstraintMessageTemplate = context.getDefaultConstraintMessageTemplate(); Object key = constraintValidatorContext.getConstraintDescriptor().getAttributes().get("key"); //禁用默认提示信息 context.disableDefaultConstraintViolation(); //设置提示语(在message前面加上key) context.buildConstraintViolationWithTemplate(key + defaultConstraintMessageTemplate).addConstraintViolation(); } } return result; } public abstract boolean doIsValid(T value, ConstraintValidatorContext context); }
调用的时候只要在JavaBean前加上@Validated注解即可
总结:上述就是在工作中遇到的问题,并扩展了Validator
原文出处:http://www.cnblogs.com/liruiloveparents/p/9378264.html



