membersound
membersound

Reputation: 86915

Simple object-to-object mapper without xml?

I'm looking for an object-to-object mapper that works without XML configurations. It should be possible to transform any simple type as well as nested lists from one object to a completely different object.

Like:

class IncomingDTO {
    String firstname;
    String lastname;
    List<Customer> customers;
}

class Customer {
    Address address;
}


class ResultDTO {
    String name; //should be a combination of firstname+lastname
    List<Address> addresses; //which might come from    
}

I'm looking for a way to not having iterate through each of the objects and copy every single entry manually. Maybe there is a library that I can give some kind of mapping configuration that does the rest for me?

Upvotes: 0

Views: 262

Answers (3)

Sidi
Sidi

Reputation: 1739

Take a look at Orika,

Orika is a Java Bean mapping framework that recursively copies (among other capabilities) data from one object to another. It can be very useful when developing multi-layered applications.

Orika on GitHub

Upvotes: 0

Frederic Close
Frederic Close

Reputation: 9649

You should have a look at apache commons beanutils http://commons.apache.org/proper/commons-beanutils/

org.apache.commons.beanutils.BeanUtils

has methods to help you like

public static void copyProperties(Object dest, Object orig)

which

Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272417

I'd prefer to do this in your Java code if possible. I'm not sure why there's a benefit to having some declaration-based solution when a code-based solution is more likely easier to read and more extensible.

If you need a framework to do this, perhaps Dozer is of use. It provides a means of identifying mappings using annotations (as well as XML)

Upvotes: 2

Related Questions