尽管字段名称相同且指定了 XML 映射,Dozer Mapper 仍未映射
Dozer Mapper does not map despite fields having same names and XML mappings specified
假设我有:
- ObjectWithList、ListWrapper 和 ObjectWithWrapper class 像这样:
//com.github.komidawi.ObjectWithList
public class ObjectWithList {
private List<String> list;
// ...
}
//com.github.komidawi.ListWrapper
public class ListWrapper {
private List<String> wrappedList;
// ...
}
//com.github.komidawi.ObjectWithWrapper
public class ObjectWithWrapper {
private ListWrapper list;
// ...
}
MyCustomConverter class 到 wrap/unwrap 这些列表。
mapperConfiguration.xml 配置文件
<!-- ... -->
<configuration>
<stop-on-errors>true</stop-on-errors>
<wildcard>true</wildcard>
<custom-converters>
<converter type="com.github.komidawi.MyCustomConverter">
<class-a>com.github.komidawi.ListWrapper</class-a>
<class-b>java.util.List</class-b>
</converter>
</custom-converters>
</converter>
<mapping>
<class-a>com.github.komidawi.ObjectWithList</class-a>
<class-b>com.github.komidawi.ObjectWithWrapper</class-b>
</mapping>
我想将 ObjectWithWrapper 映射到 ObjectWithList:
// ... creating objects
ObjectWithList objectWithList = mapper.map(objectWithWrapper, ObjectWithList.class);
objectWithList.getList(); // This is null
很遗憾,映射后,objectWithList的list字段为null。
我使用了调试器,在这种情况下,Dozer 甚至没有输入 MyCustomConverter。
令人惊讶的是,不同方向的映射效果很好:
ObjectWithWrapper objecthWithWrapper = mapper.map(objectWithList, ObjectWithWrapper.class);
objecthWithWrapper.getList(); // This is not null
总结:
- ObjectWithList 和 ObjectWithWrapper 中的字段名称相同:list
- 我已经为 Java 的 List 和 xml 文件中的 ListWrapper 指定了自定义转换器
- 我在 xml 文件
中指定了 ObjectWithList 和 ObjectWithWrapper 之间的映射
- 在这两种情况下,推土机都说:"Successfully loaded custom xml mappings from URL"
- 从 ObjectWithList 映射到 ObjectWithWrapper 时工作正常
问题是:
映射后,objectWithList 有 null 而不是实际列表。我发现它甚至没有输入 MyCustomConverter
集合很特殊,类型擦除使它们更具挑战性。如果您只想让 wrappedList 中的项目出现在生成的 ObjectWithList 实例的列表中,则无需转换器即可完成。 Dozer 将处理嵌套的 属性 映射,因此您可以通过指示 Dozer 将列表相互映射的方式来描述映射。
new BeanMappingBuilder() {
configure() {
mapping(ObjectWithList.class, ObjectWithWrapper.class,
.fields("list.wrappedList", "list")
}
}
或在XML
<mapping>
<class-a>com.github.komidawi.ObjectWithList</class-a>
<class-b>com.github.komidawi.ObjectWithWrapper</class-b>
<field>
<a>list.wrappedList</a>
<b>list</b>
</field>
</mapping>
假设我有:
- ObjectWithList、ListWrapper 和 ObjectWithWrapper class 像这样:
//com.github.komidawi.ObjectWithList
public class ObjectWithList {
private List<String> list;
// ...
}
//com.github.komidawi.ListWrapper
public class ListWrapper {
private List<String> wrappedList;
// ...
}
//com.github.komidawi.ObjectWithWrapper
public class ObjectWithWrapper {
private ListWrapper list;
// ...
}
MyCustomConverter class 到 wrap/unwrap 这些列表。
mapperConfiguration.xml 配置文件
<!-- ... -->
<configuration>
<stop-on-errors>true</stop-on-errors>
<wildcard>true</wildcard>
<custom-converters>
<converter type="com.github.komidawi.MyCustomConverter">
<class-a>com.github.komidawi.ListWrapper</class-a>
<class-b>java.util.List</class-b>
</converter>
</custom-converters>
</converter>
<mapping>
<class-a>com.github.komidawi.ObjectWithList</class-a>
<class-b>com.github.komidawi.ObjectWithWrapper</class-b>
</mapping>
我想将 ObjectWithWrapper 映射到 ObjectWithList:
// ... creating objects
ObjectWithList objectWithList = mapper.map(objectWithWrapper, ObjectWithList.class);
objectWithList.getList(); // This is null
很遗憾,映射后,objectWithList的list字段为null。 我使用了调试器,在这种情况下,Dozer 甚至没有输入 MyCustomConverter。
令人惊讶的是,不同方向的映射效果很好:
ObjectWithWrapper objecthWithWrapper = mapper.map(objectWithList, ObjectWithWrapper.class);
objecthWithWrapper.getList(); // This is not null
总结:
- ObjectWithList 和 ObjectWithWrapper 中的字段名称相同:list
- 我已经为 Java 的 List 和 xml 文件中的 ListWrapper 指定了自定义转换器
- 我在 xml 文件 中指定了 ObjectWithList 和 ObjectWithWrapper 之间的映射
- 在这两种情况下,推土机都说:"Successfully loaded custom xml mappings from URL"
- 从 ObjectWithList 映射到 ObjectWithWrapper 时工作正常
问题是:
映射后,objectWithList 有 null 而不是实际列表。我发现它甚至没有输入 MyCustomConverter
集合很特殊,类型擦除使它们更具挑战性。如果您只想让 wrappedList 中的项目出现在生成的 ObjectWithList 实例的列表中,则无需转换器即可完成。 Dozer 将处理嵌套的 属性 映射,因此您可以通过指示 Dozer 将列表相互映射的方式来描述映射。
new BeanMappingBuilder() {
configure() {
mapping(ObjectWithList.class, ObjectWithWrapper.class,
.fields("list.wrappedList", "list")
}
}
或在XML
<mapping>
<class-a>com.github.komidawi.ObjectWithList</class-a>
<class-b>com.github.komidawi.ObjectWithWrapper</class-b>
<field>
<a>list.wrappedList</a>
<b>list</b>
</field>
</mapping>