下午好,
我在Bean验证器(JSR 303)中注入服务时遇到问题。我将执行验证,但我需要在我的数据库中验证此记录;
当我使用我的服务时,它会抛出NullPointerException;
例外:
Exception in thread "main" javax.validation.ValidationException: HV000028: Unexpected exception during isValid call.
at org.hibernate.validator.internal.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:294)
at org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:164)
at org.hibernate.validator.internal.engine.ConstraintTree.validateConstraints(ConstraintTree.java:125)
at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:86)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:442)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:387)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:351)
at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:303)
at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:133)
at net.pontoall.hemisphere.core.validation.impl.TesteValidator.main(TesteValidator.java:25)
Caused by: java.lang.NullPointerException
at net.pontoall.hemisphere.core.validation.impl.UsuarioValidatorImpl.doCheckLogin(UsuarioValidatorImpl.java:48)
at net.pontoall.hemisphere.core.validation.impl.UsuarioValidatorImpl.isValid(UsuarioValidatorImpl.java:26)
at net.pontoall.hemisphere.core.validation.impl.UsuarioValidatorImpl.isValid(UsuarioValidatorImpl.java:1)
at org.hibernate.validator.internal.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:291)
... 9 more
注释:
@Target(value = ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = UsuarioValidatorImpl.class)
public @interface UsuarioValidator {
String message() default "{net.pontoall.hemisphere.validation.UsuarioValidator.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
ValidatorImpl:
public class UsuarioValidatorImpl implements ConstraintValidator<UsuarioValidator, Usuario> {
@Autowired
private IUsuarioService service;
@Override
public void initialize(UsuarioValidator arg0) {
}
@Override
public boolean isValid(Usuario entidade, ConstraintValidatorContext ctx) {
try {
return (this.doCheckLogin(entidade, ctx) && this.doCheckEmail(entidade, ctx));
} catch (HemisphereException e) {
e.printStackTrace();
return false;
}
}
private boolean doCheckLogin(Usuario entidade, ConstraintValidatorContext ctx) throws HemisphereException {
ctx.buildConstraintViolationWithTemplate("{net.pontoall.hemisphere.validation.UsuarioValidator.login.message}").addConstraintViolation();
if (entidade.getId() == null) {
// Novo Usuário
Usuario original = service.buscarPorLogin(entidade.getLogin());
if (original == null) {
return true;
}
return false;
} else {
Usuario original = service.obter(entidade.getId());
if (original == null) {
return true;
} else {
if (original.getLogin().equals(entidade.getLogin()) && original.getId().equals(entidade.getId())) {
return true;
}
}
return false;
}
}
private boolean doCheckEmail(Usuario entidade, ConstraintValidatorContext ctx) {
ctx.buildConstraintViolationWithTemplate("{net.pontoall.hemisphere.validation.UsuarioValidator.email.message}").addConstraintViolation();
if (entidade.getId() == null) {
// Novo Usuário
Usuario original = service.buscarPorEmail(entidade.getEmail());
if (original == null) {
return true;
}
return false;
} else {
Usuario original = service.obter(entidade.getId());
if (original == null) {
return true;
} else {
if (original.getEmail().equals(entidade.getEmail()) && original.getId().equals(entidade.getId())) {
return true;
}
}
return false;
}
}
}
答案 0 :(得分:12)
您应该在Spring应用程序上下文中设置LocalValidatorFactoryBean,以便通过依赖注入检索Validator
(和ValidatorFactory
)实例。
默认情况下,像这样获得的验证器使用SpringConstraintValidatorFactory
,它为约束验证器实现提供依赖注入服务,如Pangea所指出。
答案 1 :(得分:2)
如果要连接依赖项,请构建ConstraintValidatorFactory或使用SpringConstraintValidatorFactory。
答案 2 :(得分:1)
如@Pangea所述,您现在必须编写自定义 ConstraintValidatorFactory 。仅为了您的信息,Bean Validation 1.1将允许注入 ConstraintValidatorFactory 。另请参阅https://hibernate.onjira.com/browse/BVAL-238