gpt4 book ai didi

java - 配置 Hibernate Validator 不使用 hashCode()

转载 作者:行者123 更新时间:2023-12-02 13:36:46 25 4
gpt4 key购买 nike

我有以下对象来模拟对我的应用程序的请求:

@Getter
@Setter
@ToString
public class UserRequest {

@NotNull
@Size(max = 50)
private String name;

@Valid
private AddressRequest address;

}


@Getter
@Setter
@ToString
public class AddressRequest {

@Size(max = 50)
private String street;

@Size(max = 50)
private String postcode;

@NotNull
private String country;

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AddressRequest that = (AddressRequest) o;

if (street != null ? !street.equals(that.street) : that.street != null) {
return false;
}
if (postcode != null ? !postcode.equals(that.postcode) : that.postcode != null) {
return false;
}
if (country != that.country) {
return false;
}
return true;

}

@Override
public int hashCode() {
int result = street != null ? street.hashCode() : 0;
result = 31 * result + (postcode != null ? postcode.hashCode() : 0);
result = 31 * result + country.hashCode();
return result;
}
}

现在,当客户端发送 address 对象中包含 null country 的请求时,验证会触发,但我的 hashCode()。这在我这边很容易解决,只需检查其他字段是否为空国家/地区即可。但是,我宁愿避免这种情况。首先,这种情况发生在很多类中,而不仅仅是这个类。然后,我不喜欢检查空值,特别是当我知道它不应该为空时。它在我的代码中永远不会为空,由于外部工具,我将不得不修改我的代码。

我使用 Hibernate Validator 5.2。有什么方法可以配置它来避免这种情况吗?

这是来自 NPE 的堆栈跟踪:

javax.validation.ValidationException: HV000041: Call to TraversableResolver.isReachable() threw an exception.
at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1531) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.isValidationRequired(ValidatorImpl.java:1507) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:584) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:555) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:490) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:454)
...
...
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_66]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_66]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.28.jar:8.0.28]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66]
Caused by: java.lang.NullPointerException: null
at com.example.dto.AddressRequest.hashCode(AddressRequest.java:106) ~[classes/:na]
at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation$TraversableHolder.buildHashCode(CachingTraversableResolverForSingleValidation.java:143) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation$TraversableHolder.<init>(CachingTraversableResolverForSingleValidation.java:104) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation$TraversableHolder.<init>(CachingTraversableResolverForSingleValidation.java:86) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.resolver.CachingTraversableResolverForSingleValidation.isReachable(CachingTraversableResolverForSingleValidation.java:31) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
at org.hibernate.validator.internal.engine.ValidatorImpl.isReachable(ValidatorImpl.java:1522) ~[hibernate-validator-5.2.2.Final.jar:5.2.2.Final]
... 101 common frames omitted

最佳答案

因此,此问题已作为 https://hibernate.atlassian.net/browse/HV-1013 的一部分得到修复。 .

您可以将 HV 升级到最新的稳定版吗? 5.4.1.最终?

它应该是一个直接替代品(请参阅此处的迁移指南:https://developer.jboss.org/wiki/HibernateValidatorMigrationGuide)。

如果您在升级时遇到任何问题,请在此处联系我。

关于java - 配置 Hibernate Validator 不使用 hashCode(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42950808/

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