gpt4 book ai didi

spring - 在 ConstraintValidator 中注入(inject)服务 [Bean Validator - JSR 303] Spring

转载 作者:IT老高 更新时间:2023-10-28 13:53:58 25 4
gpt4 key购买 nike

下午好,

我在 Bean Validator (JSR 303) 中注入(inject)服务时遇到问题。我将执行验证,但我需要在我的数据库中验证此记录;

当我使用我的服务时,它会抛出 NullPointerException;

异常(exception):

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;
}
}
}

最佳答案

您应该设置 LocalValidatorFactoryBean在您的 Spring 应用程序上下文中,以便您可以通过依赖注入(inject)检索 Validator(和 ValidatorFactory)实例。

默认情况下,像这样获得的验证器使用 SpringConstraintValidatorFactory,它为约束验证器实现提供依赖注入(inject)服务,正如 Pangea 所指出的那样。

关于spring - 在 ConstraintValidator 中注入(inject)服务 [Bean Validator - JSR 303] Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13350537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com