Let’s say I have an object Foo which holds a list of references to object Bar:
public class Foo{
String fooStr;
List<Bar> barList;
//get/set barList/someStr go here
}
public class Bar {
String barStr;
}
Using Dozer, assuming Bar to java.util.HashMap is a trivial mapping, how do I map instances of Foo to instances of java.util.HashMap such that the referenced instances of Bar are mapped to java.util.HashMap as well? That is, I want the result of the mapping to be a HashMap with key “barList” which holds an instance of ArrayList>. Each HashMap in this list should be a mapping of Bar to HashMap.
For instance, if Foo held a single reference to Bar rather than a list, I’d do:
<mapping>
<class-a>Foo</class-a>
<class-b>java.util.Map</class-b>
<field>
<a>bar</a>
<b key="bar">this</b>
<a-hint>Bar</a-hint>
<b-hint>java.util.Map</b-hint>
</field>
</mapping>
And this would produce a hashmap that’d look like this (using JSON objects to represent HashMaps):
{
"fooStr" : "value of fooStr",
{
"barStr" : "value of barStr"
}
}
But I want to know how I can express the conversion to HashMap with a list of references to Bar such that I get this:
{
"fooStr" : "value of fooStr",
"barList" : [{ "barStr" : "bar1" }, { "barStr" : "bar2" }, ...]
}
Answers which do not use a custom mapper are preferred, but I understand if this is the only way of achieving this behavior.
You should be able to do this in a custom converter:
Warning: Code may contain bugs, but should give you the idea.