Object To Object Mapping With ModelMapper

                       Smart Techie

                 During one of mobile application development, we reused the services written for the 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 the web is a 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 bandwidth 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 a clean manner.

Follow the below steps to use ModelMapper.

  • Create a maven project.
  • Add the below dependency to pom.xml

    org.modelmapper
    modelmapper
    0.6.2

  • 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 an explicit mapping strategy provided by ModelMapper.

You can find more details here.

The source code used in this article is available here.

Advertisement

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dzone.com
DZone

DZone MVB

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