Friday 15 June 2012

How to map List to List with Dozer? -



How to map List<A> to List<B> with Dozer? -

i have service returns list of, say, a objects. want transform list of, say, b objects.

i have defined mapping a b.

naively, tried do

list<a> ayes = ... // phone call service list<b> bees = dozermapper.map(ayes, new arraylist<b>().getclass());

however, bees still list of a objects.

what should doing?

i think can accomplish in dozer using hints http://dozer.sourceforge.net/documentation/collectionandarraymapping.html, found creating wrapper around default mapper easier. illustration below. can rely on default client converters/mappers have defined mapping.

collectionmapperdecorator custom = new collectionmapperdecorator(dozermapper); collection<b> bees = custom.mapcollection(ayes, b.class); public class collectionmapperdecorator implements mapper { private mapper basemapper; public collectionmapperdecorator(mapper basemapper) { this.basemapper = basemapper; } public <t> collection<t> mapcollection(object[] source, class<t> destinationclass) { homecoming mapcollection(arrays.aslist(source), destinationclass); } public <t> collection<t> mapcollection(object[] source, collection<t> destination, class<t> destinationclass) { homecoming mapcollection(arrays.aslist(source), destination, destinationclass); } public <t> collection<t> mapcollection(collection<? extends object> source, class<t> destinationclass) { homecoming mapcollection(source, null, destinationclass); } public <t> collection<t> mapcollection(collection<? extends object> source, collection<t> destination, class<t> destinationclass) { if(destination == null) destination = new arraylist<t>(); for(object sourceobj : source) { destination.add(map(sourceobj, destinationclass)); } homecoming destination; } public <t> t map(object source, class<t> destinationclass, string mapid) throws mappingexception { homecoming basemapper.map(source, destinationclass, mapid); } public <t> t map(object source, class<t> destinationclass) throws mappingexception { homecoming basemapper.map(source, destinationclass); } public void map(object source, object destination, string mapid) throws mappingexception { basemapper.map(source, destination, mapid); } public void map(object source, object destination) throws mappingexception { basemapper.map(source, destination); } }

list map dozer

No comments:

Post a Comment