Object To Object Mapping With ModelMapper

                       Smart Techie

                 During one of  mobile application development, we reused the services written for web by exposing them as REST services for mobile. In which, we simply converted the “Object Model” used for web as JSON response for mobile.  The “Object Model” which we used for web is bit heavier and all the object properties are not required for mobile. Here, we found some of the disadvantages with heavier data transfer to the mobile client. Some of the issues are “High band width consumption to transfer the huge data over the network”, “Lot of space consumption on mobile which will ultimately hit the performance (Some times, if the app consumes more memory, during the review of the app, the respective app stores will reject the app)”. To make the “Object Model” lighter to suite to the thin clients, we wrote some converters which will copy the required properties from POJOs to the “Object Model” used for mobile. Later, we found there is a “Model Mapper” framework to do this job in clean manner.

Follow the below steps to use ModelMapper.

  • Create a maven project.
  • Add the below dependency to pom.xml
<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>0.6.2</version>
</dependency>
  • Create ModelMapper object.
ModelMapper modelMapper = new ModelMapper();
  • Pass the source object and the destination class to the ModelMapper’s map method.
modelMapper.map(sourceUser, DestUser.class);

In the above code, ModelMapper will analyze source and destination object properties to determine which properties implicitly match according to a matching strategy.

If you have some complex structure of objects, then you can use explicit mapping strategy provided by ModelMapper.

You can find more details here.

The source code used in this article is available here.

Advertisements

I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

Tagged with: ,
Posted in ModelMapper
One comment on “Object To Object Mapping With ModelMapper
  1. Saleem says:

    It’s really a cool and helpful piece of information.
    I am happy that you shared this helpful info with us. Please keep us up to date like
    this. Thank you for sharing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

DZone

DZone MVB

Java Code Geeks
Java Code Geeks
%d bloggers like this: