Using ModelMapper in Spring Boot

Tram Ho

Today’s article will be how to use the ModelMapper library to map back and forth between objects in Spring. The home page of ModelMapper is here http://modelmapper.org/ , easy to understand for those who want to learn more.

1. Introducing ModelMapper

1.1. What is ModelMapper?

ModelMapper is a Java library, which simplifies code mapping objects. Mapping can be understood as converting between two objects with similar structure. In Spring Boot there are different types of data, but the structure is almost similar, so this library can also be used with them. Example map between entity and model, entity and DTO, …

For example, I have two classes User (entity) and UserDto quite similar, but UserDto does not have a password field as follows.

Using the ModelMapper library, just a few short lines of code are needed as follows.

Previously, if you did not use model mapper, you had to copy each field between two classes. It was lengthy and unnecessary, but the speed was the fastest. There is also a need to further consider whether to use ModelMapper or not.

2. Usage

2.1. Add to project

Adding ModelMapper to the Spring Boot project is similar to other dependencies. If the project uses Maven, just add this part to pom.xml file

In Gradle it looks like this.

Or you can download JAR here http://modelmapper.org/downloads/ .

2.2. Instance creation and usage

There are no detailed instructions on how to organize in source code on the ModelMapper homepage. Simply say, you have to create an object of ModelMapper first. Every operation uses this object, so ideally we will create a bean for it as well.

I create a configuration file ModelMapperConfig.java as follows.

The above code uses @Configuration and @Bean to define the bean. You can review this article to better understand Lifecycle, beans and component scan mechanism .

After that, whenever you need to, get the ModelMapper bean to use.

3. Few things to note

3.1. Mapping levels

Also known as the Matching strategy, details are available here http://modelmapper.org/user-manual/configuration/#matching-strategies . In short, there are 3 types of mapping, each with a constant representation (to set config):

  • Standard map strategy: MatchingStrategies.STANDARD
  • Loose map strategy: MatchingStrategies.LOOSE
  • Tight map strategy: MatchingStrategies.STRICT

This comparison is quite difficult to say, so I wrote down a few examples here.

Criterion 1: Order of words in attribute names:

  • Standard: don’t care about the order
  • Loose: regardless of the order
  • Strict: The word order must be correct

Criterion 2: Source property:

  • Standard: every word in the source attribute name must exist in the target attribute name.
  • Loose: the last word must be in the target name
  • Strict: every word in the source attribute must match all words

Criterion 3: Destination property:

  • Standard: all words of the target attribute must match
  • Loose: the last word in the target attribute is required
  • Strict: every word in the target property must match. To configure, use the ModelMapper.getConfiguration().setMatchingStrategy() as shown in the code below.

I encourage you to use Standard or Strict . In case of force majeure, use Loose . Generally speaking, this is a bit of a headache, so I just said it briefly.

3.2. Customizable map configuration

The mapping configuration can be adjusted by setting the properties of ModelMapper.getConfiguration() .

Some basic configuration properties are as follows:

  • setSkipNullEnabled() allows null property to be omitted
  • setDeepCopyEnabled() defaults to shallow copy, but using a deep copy will be slower.
  • setMatchingStrategy() set mapping type (like above)
  • setFieldAccessLevel() specifies to what extent the field can be accessed (private, public, …)
  • setMethodAccessLevel() specifies the level of method, getter, and setter access (as shown above).
  • setSourceNameTokenizer() specifies the naming convention for the property at source (the source object is used to map).
  • setDestinationNameTokenizer() specifies a naming convention for the property on the target (object map out).

3.3. Should use getter, setter

Most of the model object in Spring Boot only public getter setter out, the fields are kept private. Therefore, when using ModelMapper, it is the same:

  • The source object needs a getter
  • The target object needs a setter

3.4. Always test

When using the model mapper, it is imperative to check the results of the map again.

Why, the reason is simple because we do not know the process of the model mapper doing the mapping of objects. Therefore, it may happen that the map cannot be map, the map is missing the field, the map is incorrect. Hence, always remember this.

Always test to check if ModelMapper has the correct map.

Test, you can write Unit test, debug or use the validation feature included in ModelMapper. Details can be found here http://modelmapper.org/user-manual/validation/ .

In addition to the above features, ModelMapper has many other cool features. You can find out more on its homepage.


Okay here is the end of the post. Remember to upvote or clip my post if it feels good. Dear.

Share the news now

Source : Viblo