如何将 HashMap 映射到 Pojo
我在使用推土机将 HashMap 映射到 Pojo 时遇到困难。注意:我的 Pojo 中的属性名称不会映射到我填充的 HashMap 中的键值。我正在尝试使用 dozer 映射文件中的 set-method 属性将哈希键映射到 Pojo setter。当我运行下面的代码时,我没有收到任何异常,但 Pojo 没有填充哈希中的任何数据。任何建议或反馈将不胜感激。
Pojo:
public class Hotel {
private String companyAssignedId;
public Hotel(){}
public String getCompanyAssignedId() {
return companyAssignedId;
}
public void setCompanyAssignedId(String companyAssignedId) {
this.companyAssignedId = companyAssignedId;
}
}
映射 XML:
<mapping>
<class-a>com.reardencommerce.platformsvc.hadoop.dto.Hotel</class-a>
<class-b>java.util.Map</class-b>
<field>
<a set-method="setCompanyAssignedId">hotel</a>
<b key="COMPANY_ASSG_ID">rawData</b>
</field>
</mapping>
执行代码:
DozerBeanMapper beanMapper = new DozerBeanMapper();
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer-test.xml");
beanMapper.setMappingFiles(mappingFiles);
Map<String, String> rawData = new HashMap <String, String> ();
rawData.put("COMPANY_ASSG_ID","12345");
Hotel hotel = new Hotel();
beanMapper.map(rawData, hotel);
I'm having difficulties mapping a HashMap to a Pojo using dozer. Note: the attribute names in my Pojo don't map to the key values in HashMap i've populated. I'm trying to use the set-method attribute in the dozer mapping file to map the hash key to the Pojo setter. When I run the code below, I don't get any exceptions, but the the Pojo isn't populated with any data from the hash. Any suggestions or feedback would be appreciated.
Pojo:
public class Hotel {
private String companyAssignedId;
public Hotel(){}
public String getCompanyAssignedId() {
return companyAssignedId;
}
public void setCompanyAssignedId(String companyAssignedId) {
this.companyAssignedId = companyAssignedId;
}
}
Mapping XML:
<mapping>
<class-a>com.reardencommerce.platformsvc.hadoop.dto.Hotel</class-a>
<class-b>java.util.Map</class-b>
<field>
<a set-method="setCompanyAssignedId">hotel</a>
<b key="COMPANY_ASSG_ID">rawData</b>
</field>
</mapping>
Execution Code:
DozerBeanMapper beanMapper = new DozerBeanMapper();
List<String> mappingFiles = new ArrayList<String>();
mappingFiles.add("dozer-test.xml");
beanMapper.setMappingFiles(mappingFiles);
Map<String, String> rawData = new HashMap <String, String> ();
rawData.put("COMPANY_ASSG_ID","12345");
Hotel hotel = new Hotel();
beanMapper.map(rawData, hotel);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
this
而不是rawData
。 Dozer 会在 hashmap 中寻找 rawData 字段的读/写方法,最终会出现映射异常。Try using
this
instead ofrawData
. Dozer would look for a read/write method for the fieldrawData
in hashmap and would end up with a mapping exception.