在验证对象时,我在 Hibernate Validator 6.0.9.Final 中遇到一些奇怪的行为。给定以下 POJO:
public class Log {
private Long uid;
@javax.validation.constraints.NotEmpty
@javax.validation.constraints.NotNull
private String value;
private long reference;
private String type;
private String createdDt;
private String createdBy;
private Boolean actioned;
private Long dxr;
// Constructors..
// Getters and setters...
// Equals and hashcode overrides...
}
现在,如果我使用新实例 JUnit 测试验证实体,我会得到以下输出:
@Test
public void test() {
Log cLog = new Log();
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator beanvalidator = validatorFactory.getValidator();
Set<ConstraintViolation<CommunicationLog>> constraintViolations = beanvalidator.validate(cLog);
System.out.println(constraintViolations);
}
[ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=value, rootBeanClass=class Log, messageTemplate='{javax.validation.constraints.NotNull.message}'},
ConstraintViolationImpl{interpolatedMessage='must not be empty', propertyPath=value, rootBeanClass=class Log, messageTemplate='{javax.validation.constraints.NotEmpty.message}'}]
这很好。但是,如果我在 Hibernate/Jersey 项目上运行此相同代码,则仅运行 @NotNull
验证。我似乎无法让 @NotEmpty
validator 在对象上运行。
我尝试删除 @NotNull
validator (因为 @NotEmpty
validator 会处理它),但为了完整起见,我将其保留,以证明 @NotEmpty
validator 没有返回上图中的任何内容。
我不明白为什么它在部署到 Web 项目时没有运行,但在 JUnit 测试下运行时运行良好。我在这里缺少什么吗?
最佳答案
好的,我已经解决了这个问题。我没有意识到我已将项目部署到错误的服务器上。我已将其部署到 Payara 4.1.1.171 服务器而不是 Payara 5.181 服务器。然而,我希望能够显示某种信息——也许是在部署时。
关于java - Hibernate Validator 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51398670/