sách gpt4 ăn đã đi

Không thể ánh xạ hai chiều Bulldozer của (Chuỗi, Chuỗi) bằng trình chuyển đổi tùy chỉnh?

In lại 作者:搜寻专家 更新时间:2023-11-01 03:29:12 27 4
mua khóa gpt4 giày nike

我有一个带有自定义转换器的 Dozer 映射:


com.xyz.Customer
com.xyz.CustomerDAO

customerName
customerName


和转换器:

public class DozerEmptyString2NullConverter extends DozerConverter {

public DozerEmptyString2NullConverter() {
super(String.class, String.class);
}

public String convertFrom(String source, String destination) {
String ret = null;
if (source != null) {
if (!source.equals(""))
{
ret = StringFormatter.wildcard(source);
}
}
return ret;
}

public String convertTo(String source, String destination) {
return source;
}
}

当我在一个方向(Customer -> CustomerDAO)调用映射器时,方法“convertTo”被调用。

由于 Dozer 能够处理双向映射,我希望一旦我在相反方向调用映射器,就会调用方法“convertFrom”。

但是永远不会调用 convertTo 方法。

我怀疑问题是,这两种类型都是字符串 - 但我怎样才能让它工作?

作为一种解决方法,我创建了两个单向映射,这是标准解决方案,还是该行为是一个错误?

câu trả lời hay nhất

是的,问题是你的源类和目标类是一样的。这是推土机 nguồnDozerConverter:

  public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class destinationClass, Class sourceClass) {
Class wrappedDestinationClass = ClassUtils.primitiveToWrapper(destinationClass);
Class wrappedSourceClass = ClassUtils.primitiveToWrapper(sourceClass);

if (prototypeA.equals(wrappedDestinationClass)) {
return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue);
} else if (prototypeB.equals(wrappedDestinationClass)) {
return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue);
} else if (prototypeA.equals(wrappedSourceClass)) {
return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue);
} else if (prototypeB.equals(wrappedSourceClass)) {
return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue);
} else if (prototypeA.isAssignableFrom(wrappedDestinationClass)) {
return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue);
} else if (prototypeB.isAssignableFrom(wrappedDestinationClass)) {
return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue);
} else if (prototypeA.isAssignableFrom(wrappedSourceClass)) {
return convertTo((A) sourceFieldValue, (B) existingDestinationFieldValue);
} else if (prototypeB.isAssignableFrom(wrappedSourceClass)) {
return convertFrom((B) sourceFieldValue, (A) existingDestinationFieldValue);
} khác {
throw new MappingException("Destination Type (" + wrappedDestinationClass.getName()
+ ") is not accepted by this Custom Converter ("
+ this.getClass().getName() + ")!");
}

}

不是使用 convertFromconvertTo 方法(它们是新 API 的一部分),而是按照原来的方式执行,您必须实现 CustomConverter。转换giốngtutorial所示.

关于java - 带有自定义转换器的推土机双向映射(字符串,字符串)不可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5041832/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress