英文:
ModelMapper LinkedHashMap to LinkedHashMap ArrayIndexOutOfBoundsException
问题
以下是您要翻译的内容:
"I've been working on a project where some new status data is fetched into an object and applied via ModelMapper on the current object status data.
Fields are matched and updated automatically which is nice.
Now I've added some more complex data in the form of a List<Map<String, String>>.
And things are breaking.
Here is the simplified code :
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<>();
l1.add(m1);
List<Map<String, String>> l2 = new ArrayList<>();
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();
}
}
}
Things are working alright if typeB listOfMap is Empty.
But if we already have another list of values in listOfMap on object typeB.
Then we get the following error when trying to map A to 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
What is happening here?
Can I fix this code to make ModelMapper map List<Map<String, String>> type correctly ?
thx all"
英文:
I've been working on a project where some new status data is fetched into an object and applied via ModelMapper on the current object status data.
Fields are matched and updated automatically which is nice.
Now I've added some more complex data in the form of a List<Map<String, String>>.
And things are breaking.
Here is the simplified code :
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();
}
}
}
Things are working alright if typeB listOfMap is Empty.
But if we already have another list of values in listOfMap on object typeB.
Then we get the following error when trying to map A to 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
What is happening here?
Can I fix this code to make ModelMapper map List<Map<String, String>> type correctly ?
thx all
答案1
得分: 0
-
我不知道为什么您要使用
modelmapper.map(typeA, typeB)
而不是modelmapper.map(typeA, TypeB.class)
链接。 -
您可以为复杂数据使用自定义映射:
public class TestModelMapper extends ModelMapper {
TestModelMapper() {
super();
this.initMappings();
}
private void initMappings() {
Converter<List<Map<String, String>>, List<Map<String, String>>> converter = new Converter<>() {
public List<Map<String, String>> convert(MappingContext<List<Map<String, String>>, List<Map<String, String>>> context) {
return context.getSource() == null ? null : new ArrayList<>(context.getSource());
}
};
PropertyMap<TypeA, TypeB> 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<String, String> m1 = new LinkedHashMap();
m1.put("test1", "machin");
m1.put("test2", "truc");
m1.put("test3", "bidule");
List<Map<String, String>> l1 = new ArrayList<Map<String, String>>();
l1.add(m1);
typeA.setListOfMap(l1);
ModelMapper modelmapper = new TestModelMapper();
try {
modelmapper.map(typeA, TypeB.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
英文:
- I don't know why you use modelmapper.map(typeA, typeB) instead of modelmapper.map(typeA, TypeB.class) http://modelmapper.org/getting-started/
- You can use your own mappings for complex data:
<pre>
public class TestModelMapper extends ModelMapper {
TestModelMapper() {
super();
this.initMappings();
}
private void initMappings() {
Converter<List<Map<String, String>>, List<Map<String, String>>> converter = new Converter<>() {
public List<Map<String, String>> convert(MappingContext<List<Map<String, String>>, List<Map<String, String>>> context) {
return context.getSource() == null ? null : new ArrayList<>(context.getSource());
}
};
PropertyMap<TypeA, TypeB> 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<String, String> m1 = new LinkedHashMap();
m1.put("test1","machin");
m1.put("test2","truc");
m1.put("test3","bidule");
List<Map<String, String>> l1 = new ArrayList<Map<String, String>>();
l1.add(m1);
typeA.setListOfMap(l1);
ModelMapper modelmapper = new TestModelMapper();
try {
modelmapper.map(typeA, TypeB.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
</pre>
答案2
得分: 0
在当时,我正在运行此代码,使用了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}]
英文:
At the time, I was running this code with modelmapper dependency version 2.3.8.
Updating to the latest release 2.4.2 fixes this problem.
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();
}
Ouputs as expected :
[{test1=machin, test2=truc}]
[{test1=machin, test2=truc, test3=bidule}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论