Добрый день,
У меня возникла проблема с внедрением службы в валидаторе 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;
}
}
}