ModelMapper LinkedHashMap 到 LinkedHashMap ArrayIndexOutOfBoundsException
ModelMapper LinkedHashMap to LinkedHashMap ArrayIndexOutOfBoundsException
我一直在从事一个项目,其中一些新的状态数据被提取到一个对象中,并通过 ModelMapper 应用于当前对象状态数据。
字段自动匹配和更新,这很好。
现在我以 List
这是简化的代码:
public class TypeA {
private List<Map<String, String>> listOfMap;
}
public class TypeB {
private List<Map<String, String>> listOfMap;
}
public class Test {
public static void main(String[] args) {
TypeA typeA = new TypeA();
TypeB typeB = new TypeB();
Map<String, String> m1 = new LinkedHashMap();
m1.put("test1","machin");
m1.put("test2","truc");
m1.put("test3","bidule");
Map<String, String> m2 = new LinkedHashMap();
m2.put("test1","machin");
m2.put("test2","truc");
List<Map<String, String>> l1 = new ArrayList<Map<String, String>>();
l1.add(m1);
List<Map<String, String>> l2 = new ArrayList<Map<String, String>>();
l2.add(m2);
typeA.setListOfMap(l1);
typeB.setListOfMap(l2); // this can be set to null to fix the problem
ModelMapper modelmapper = new ModelMapper();
try {
modelmapper.map(typeA, typeB);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果 typeB listOfMap 为空,一切正常。
但是如果我们已经在对象类型 B 的 listOfMap 中有另一个值列表。
然后在尝试将 A 映射到 B 时出现以下错误:
1) Converter org.modelmapper.internal.converter.MapConverter@1f32e575 failed to convert java.util.LinkedHashMap to java.util.LinkedHashMap.
1 error
at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:380)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:81)
at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:573)
at org.modelmapper.ModelMapper.map(ModelMapper.java:447)
at modelmapper_debug.Main.main(Main.java:30)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at org.modelmapper.internal.converter.MapConverter.convert(MapConverter.java:59)
at org.modelmapper.internal.converter.MapConverter.convert(MapConverter.java:1)
at org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:303)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:110)
at org.modelmapper.internal.converter.MergingCollectionConverter.convert(MergingCollectionConverter.java:59)
at org.modelmapper.internal.converter.MergingCollectionConverter.convert(MergingCollectionConverter.java:1)
at org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:303)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:110)
at org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:242)
at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:188)
at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:152)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:115)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:72)
... 3 more
这里发生了什么?
我可以修复此代码以使 ModelMapper map List
谢谢大家
- 我不知道你为什么使用 modelmapper.map(typeA, typeB) 而不是 modelmapper.map(typeA, TypeB.class) http://modelmapper.org/getting-started/
- 您可以对复杂数据使用自己的映射:
public class TestModelMapper extends ModelMapper {
TestModelMapper() {
super();
this.initMappings();
}
private void initMappings() {
Converter>, List>> converter = new Converter() {
public List> convert(MappingContext>, List>> context) {
return context.getSource() == null ? null : new ArrayList(context.getSource());
}
};
PropertyMap entityToDTOMap = new PropertyMap() {
protected void configure() {
using(converter).map(source.getListOfMap()).setListOfMap(null);
}
};
this.addMappings(entityToDTOMap);
}
}
public class Test {
public static void main(String[] args) {
TypeA typeA = new TypeA();
TypeB typeB = new TypeB();
Map m1 = new LinkedHashMap();
m1.put("test1","machin");
m1.put("test2","truc");
m1.put("test3","bidule");
List> l1 = new ArrayList>();
l1.add(m1);
typeA.setListOfMap(l1);
ModelMapper modelmapper = new TestModelMapper();
try {
modelmapper.map(typeA, TypeB.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当时,我是运行这段代码,modelmapper依赖版本为2.3.8。
更新到最新版本 2.4.2 修复了这个问题。
ModelMapper modelmapper = new ModelMapper();
try {
System.out.println(typeB.getListOfMap().toString());
modelmapper.map(typeA, typeB);
System.out.println(typeB.getListOfMap().toString());
} catch (Exception e) {
e.printStackTrace();
}
如预期的输出:
[{test1=machin, test2=truc}]
[{test1=machin, test2=truc, test3=bidule}]
我一直在从事一个项目,其中一些新的状态数据被提取到一个对象中,并通过 ModelMapper 应用于当前对象状态数据。 字段自动匹配和更新,这很好。
现在我以 List
这是简化的代码:
public class TypeA {
private List<Map<String, String>> listOfMap;
}
public class TypeB {
private List<Map<String, String>> listOfMap;
}
public class Test {
public static void main(String[] args) {
TypeA typeA = new TypeA();
TypeB typeB = new TypeB();
Map<String, String> m1 = new LinkedHashMap();
m1.put("test1","machin");
m1.put("test2","truc");
m1.put("test3","bidule");
Map<String, String> m2 = new LinkedHashMap();
m2.put("test1","machin");
m2.put("test2","truc");
List<Map<String, String>> l1 = new ArrayList<Map<String, String>>();
l1.add(m1);
List<Map<String, String>> l2 = new ArrayList<Map<String, String>>();
l2.add(m2);
typeA.setListOfMap(l1);
typeB.setListOfMap(l2); // this can be set to null to fix the problem
ModelMapper modelmapper = new ModelMapper();
try {
modelmapper.map(typeA, typeB);
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果 typeB listOfMap 为空,一切正常。
但是如果我们已经在对象类型 B 的 listOfMap 中有另一个值列表。
然后在尝试将 A 映射到 B 时出现以下错误:
1) Converter org.modelmapper.internal.converter.MapConverter@1f32e575 failed to convert java.util.LinkedHashMap to java.util.LinkedHashMap.
1 error
at org.modelmapper.internal.Errors.throwMappingExceptionIfErrorsExist(Errors.java:380)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:81)
at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:573)
at org.modelmapper.ModelMapper.map(ModelMapper.java:447)
at modelmapper_debug.Main.main(Main.java:30)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at org.modelmapper.internal.converter.MapConverter.convert(MapConverter.java:59)
at org.modelmapper.internal.converter.MapConverter.convert(MapConverter.java:1)
at org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:303)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:110)
at org.modelmapper.internal.converter.MergingCollectionConverter.convert(MergingCollectionConverter.java:59)
at org.modelmapper.internal.converter.MergingCollectionConverter.convert(MergingCollectionConverter.java:1)
at org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:303)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:110)
at org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:242)
at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:188)
at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:152)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:115)
at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:72)
... 3 more
这里发生了什么?
我可以修复此代码以使 ModelMapper map List
谢谢大家
- 我不知道你为什么使用 modelmapper.map(typeA, typeB) 而不是 modelmapper.map(typeA, TypeB.class) http://modelmapper.org/getting-started/
- 您可以对复杂数据使用自己的映射:
public class TestModelMapper extends ModelMapper { TestModelMapper() { super(); this.initMappings(); } private void initMappings() { Converter>, List>> converter = new Converter() { public List> convert(MappingContext>, List>> context) { return context.getSource() == null ? null : new ArrayList(context.getSource()); } }; PropertyMap entityToDTOMap = new PropertyMap() { protected void configure() { using(converter).map(source.getListOfMap()).setListOfMap(null); } }; this.addMappings(entityToDTOMap); } } public class Test { public static void main(String[] args) { TypeA typeA = new TypeA(); TypeB typeB = new TypeB(); Map m1 = new LinkedHashMap(); m1.put("test1","machin"); m1.put("test2","truc"); m1.put("test3","bidule"); List> l1 = new ArrayList>(); l1.add(m1); typeA.setListOfMap(l1); ModelMapper modelmapper = new TestModelMapper(); try { modelmapper.map(typeA, TypeB.class); } catch (Exception e) { e.printStackTrace(); } } }
当时,我是运行这段代码,modelmapper依赖版本为2.3.8。
更新到最新版本 2.4.2 修复了这个问题。
ModelMapper modelmapper = new ModelMapper();
try {
System.out.println(typeB.getListOfMap().toString());
modelmapper.map(typeA, typeB);
System.out.println(typeB.getListOfMap().toString());
} catch (Exception e) {
e.printStackTrace();
}
如预期的输出:
[{test1=machin, test2=truc}]
[{test1=machin, test2=truc, test3=bidule}]