我有以下文件- 模型类具有类型为java.util.Map的“地图”字段。此地图填充了一个Cost对象。
在目标端,我有GMCModel类,该类具有GooglePrice实例变量。我正在尝试使用给定的映射将Cost对象从源Model对象中的映射映射到GooglePrice。我找不到任何文档。无需编写自定义转换器,有人可以告诉我是否可行。
这是蚀项目-https://github.com/gitterygit/Dozer
package com.vinod.map.nested;
import java.util.Map;
public class Model {
public Model(){
}
String prop1 ;
public String getProp1() {
return prop1;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
public String getProp2() {
return prop2;
}
public void setProp2(String prop2) {
this.prop2 = prop2;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
String prop2 ;
Map map ;
}
package com.vinod.map.nested;
public class Cost {
public String currency;
public float value;
public String getCurrency() {
System.out.println("Get Currency Getting Called");
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
@Override
public String toString() {
return "Cost [currency=" + currency + ", value=" + value + "]";
}
}
package com.vinod.map.nested;
public class GMCModel {
public GMCModel(){
}
public java.lang.String getAvailability() {
return availability;
}
public void setAvailability(java.lang.String availability) {
this.availability = availability;
}
public java.util.List<java.lang.String> getAdditionalImageLinks() {
return additionalImageLinks;
}
public void setAdditionalImageLinks(java.util.List<java.lang.String> additionalImageLinks) {
this.additionalImageLinks = additionalImageLinks;
}
public java.lang.Boolean getAdult() {
return adult;
}
public void setAdult(java.lang.Boolean adult) {
this.adult = adult;
}
private java.lang.String availability;
private java.util.List<java.lang.String> additionalImageLinks;
private java.lang.Boolean adult;
private String prop2;
private String prop3;
private GooglePrice price;
@Override
public String toString() {
return "GMCModel [availability=" + availability + ", additionalImageLinks=" + additionalImageLinks + ", adult="
+ adult + ", prop2=" + prop2 + ", prop3=" + prop3 + ", price=" + price + "]";
}
public GooglePrice getPrice() {
return price;
}
public void setPrice(GooglePrice price) {
this.price = price;
}
public String getProp3() {
return prop3;
}
public void setProp3(String prop3) {
this.prop3 = prop3;
}
public String getProp2() {
return prop2;
}
public void setProp2(String prop2) {
this.prop2 = prop2;
}
}
package com.vinod.map.nested;
public class GooglePrice {
public GooglePrice(){
}
public String country;
public float value;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public float getValue() {
return value;
}
public void setValue(float value) {
this.value = value;
}
@Override
public String toString() {
return "GooglePrice [country=" + country + ", value=" + value + "]";
}
}
package com.vinod.map.nested;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.dozer.DozerBeanMapper;
public class MapDemoForGMC {
public static void main(String[] args) {
DozerBeanMapper dozerMapper = new DozerBeanMapper();
dozerMapper.setMappingFiles(Arrays.asList("mappings\\demo_model_gmc.xml"));
Model model = new Model();
model.setProp1("FirstOne");
model.setProp2("SecondOne");
Map modelmap = new HashMap();
modelmap.put("mapkey1", "mapVal1");
modelmap.put("mapkey2", "mapVal2");
modelmap.put("mapkey3", "mapVal3");
Cost c = new Cost();
c.setCurrency("INR");
c.setValue(12);
modelmap.put("cost", c);
model.map=modelmap;
GMCModel gmc = dozerMapper.map(model, GMCModel.class);
System.out.println(gmc);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net
http://dozer.sourceforge.net/schema/beanmapping.xsd">
<mapping>
<class-a>com.vinod.map.nested.Cost</class-a>
<class-b>com.vinod.map.nested.GooglePrice</class-b>
<field>
<a>currency</a>
<b>country</b>
</field>
<field>
<a>value</a>
<b>value</b>
</field>
</mapping>
<mapping wildcard="false">
<class-a>com.vinod.map.nested.Model</class-a>
<class-b>com.vinod.map.nested.GMCModel</class-b>
<field>
<a>prop1</a>
<b>availability</b>
</field>
<field>
<a>prop2</a>
<b>prop2</b>
</field>
<field>
<a key="mapkey1">map</a>
<b>prop3</b>
</field>
<field >
<a key="cost">map</a>
<b>price</b>
<a-hint>com.vinod.map.nested.Cost</a-hint>
<b-hint>com.vinod.map.nested.GooglePrice</b-hint>
</field>
</mapping>
</mappings>