- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个带有自定义转换器的 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ồnvì DozerConverter
:
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() + ")!");
}
}
不是使用 convertFrom
Và convertTo
方法(它们是新 API 的一部分),而是按照原来的方式执行,您必须实现 CustomConverter。转换
giốngtutorial所示.
关于java - 带有自定义转换器的推土机双向映射(字符串,字符串)不可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5041832/
我有一个类似于以下两个类的场景: public class Person { private String name; private Set familyMembers; /
如何将单个字段映射到 Dozer 中的 List/Collection? class SrcFoo { private String id; private List bars; }
我正在通过 Dozer 映射器映射两个 DTO 对象。我有兴趣从列表中选择一个值并将其映射到目标文件中的单个字段。 是否可以像这样使用映射: someList[0] someVariab
我正在使用推土机来映射对象。如何忽略(排除)使用推土机注释的字段? 类似于: class A { @IgnoreField public String so
我已经被这个麻烦困扰了两天了,但我无法解决。我有: public class ClientBo{ ... List person; ... } 和 public class ClientV
为什么dozer无法将Number类型的Source Class变量映射到相同类型的Destination Class变量? 出现以下异常: java.lang.IllegalArgumentExce
如何以编程方式为推土机设置自定义转换器?以下代码不起作用: 自定义转换器实现: class ConverterImpl extends DozerConverter { ConverterImpl()
我需要编译一个针对特定架构或更好的程序,使用通用 AMD64 指令编译,忽略更新的 AMD cpu(推土机和打桩机)。 具体来说,我需要绝对忽略任何 FMA 或 XOP 指令(我的理解是这些通常在 O
我想将DTO(均为字符串数据类型)映射到VO(包含String、int、boolean、Date) 学生DTO private StudentDetailDTO student; 学生详细信息DTO:
我有一个列表列表,我正在尝试使用 Dozer 和自定义转换器将其映射到二维数组 [][]。 public class Field { List items; public void a
有没有办法通过它的 xml 映射文件来配置 dozer 以将 -1 的原始 int 字段值转换为空对象引用? 遗留对象模型默认值为 -1,因此零可以是一个有效的选择。 我们要映射到的较新的对象模型假定
我正在尝试在我当前运行 Spring 和 Hibernate 的 Web 应用程序上实现 Dozer 作为桥接模式的帮助,但我认为我做错了什么,因为尝试多次使用 DozerBeanMapper 实例会
Tôi là một lập trình viên xuất sắc, rất giỏi!