Spring Boot ModelMapper

One additional tool to the developers tool belt.

In many applications that I had work on we had this dual model representation, through the domain model and DTO. Moreover in many other situations when working with EAI applications those DTO might have been in different “flavours” – especially when you need to pass data from multiple systems that define completely different format. Object to object mapping is fairly common and trivial task to do, and I personally favor to leverage automated tools like Dozer, MapStruct, Orika or ModelMapper. ModelMapper is my personal favorite in this matter. It relays on reasonable mapping strategy and naming conventions. Limiting the need for specifying the explicit mapping between object in Java or XML as other tools might require. I had been using it so often in multiple setups, that I had decided to build Spring Boot integration on top of it.

This is why another small project emerged: https://github.com/jmnarloch/modelmapper-spring-boot-starter

Both Spring Boot and ModelMapper works based on convention over configuration, which sometimes might lead to subtle bugs, that might be hard to discover, especially if you haven’t covered all possible edge cases with your tests. Although the benefits of using both of them are undeniable. In most cases you just need to add them on the classpath to be able to leverage all of their features. With the ModelMapper Boot starter you can setup object mapping as easily in your project.

To setup you project add the fallowing dependency to your POM or Gradle build file:

<dependency>
    <groupId>com.github.jmnarloch</groupId>
    <artifactId>modelmapper-spring-boot-starter</artifactId>
    <version>1.0.1</version>
</dependency>

This is everything that you need to do – no additional XML, no explicit Java mapping, no boilerplate code for copying one object properties to another. Whenever you need you can now inject the ModelMapper instance into your beans and map your objects.

For additional information and configuration options please see the readme: https://github.com/jmnarloch/modelmapper-spring-boot-starter

One comment

  1. ida · 14 Days Ago

    when using springboot devTools and trying redeploy the project (no metter where the fix is), the modelMapper throws an exception: ClassCastException dto.StudentDTO$$EnhancerByModelMapper$$1d67328a cannot be cast to dto.StudentDTO.
    the only solution that i found is to restart the tomcat server.
    do you have an idea?

    Like

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