根据 Glassfish 4.0 wiki,Glassfish 4.0 应该包含 JSR349 Bean Validation 1.1。:GF4 wiki 链接
根据 JSR349 规范,CDI 注入应该开箱即用:Bean Validation 1.1。CDI 集成
所以我相应地改变了我的 pom.xml:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>provided</scope>
</dependency>
并尝试将 CDI Bean 注入 ConstraintValidator:
public class UniqueEmaiValidator implements ConstraintValidator<UniqueEmail, String> {
@Inject
private UserAccountService accountService;
@Override
public void initialize(UniqueEmail constraintAnnotation) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return !accountService.userExistsByEmail(value);
}
}
但是,在测试应用程序时(使用 arquillian-glassfish-remote-3.1 1.0.0.CR4 运行 Arquillian 1.1.1),验证总是会失败,因为userAccountService
它为 null,因此最终会抛出一个 错误NullPointerException
。
为了使 Bean Validation 1.1 正常工作,我缺少什么?
编辑:
A) 可以确认它不是由 Arquillian 远程测试引起的 - 也会引发 NPEx。在服务器上运行时
B) 在 GlassFish Server Open Source Edition 4.0 (build 89) 上运行
C) 我使用 Hibernate Validator 的 5.0.1.FINAL 明确地重新构建了 bean-validation.jar。mvn package
输出:
[INFO] Building Validation API (JSR 349) version 1.1.0.Final, Hibernate Validator version 5.0.1.Final and its dependencies repackaged as OSGi bundle 2.1.92
GlassFish 服务器启动后,我收到以下信息:
INFO: GlassFish Server Open Source Edition 4.0 (89) startup time : Felix (5,736ms), startup services(2,078ms), total(7,814ms)
INFO: HV000001: Hibernate Validator 5.0.1.Final
所以我想重建确实奏效了。但是,它并没有解决我的 NullPointerException 问题:/
D)@Gunnar
这是实体类,使用@Constraint
注释:
@Entity
public class UserAccount extends AbstractEntity implements VisibilitySettings {
@UniqueEmail
private String email;
[...]
}
注释本身:
@Constraint(validatedBy = {UniqueEmailValidator.class})
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UniqueEmail {
String message() default "{validator.security.useraccount.emailexists}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
以及相应的ConstraintValidator
:
public class UniqueEmailValidator implements ConstraintValidator<UniqueEmail, String> {
@Inject
private UserAccountService accountService;
@Override
public void initialize(UniqueEmail constraintAnnotation) {
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return !accountService.userExistsByEmail(value);
}
}
UserAccountService
注释为@ApplicationScoped @Transactional