Home » Framework » Conversion between DTO to Entity using ModelMapper

Conversion between DTO to Entity using ModelMapper

In the previous article, we have seen conversion between Entity to DTO. In this article let us see the vise versa conversion. Here also I used Spring Boot with ModelMapper setup to explain DTO to Entity conversion.

Converting DTO to Entity used only when the data sent from client to server. Simply say while update or save we need this type of conversion. Now let us see how to implement it.

In the last article, I explained the ModelMapper setup with Spring Boot. If you want to know that please take a look at ModelMapper setup and DTO conversion.

DTO to Entity Conversion

The below snippet is an example for DTO to Entity conversion. Here also used ProfileDTO and ProfileEntity classes.

@Autowired
private static ModelMapper modelMapper;

//This function converts and returns the Dto to Entity
private static ProfileEntity convertDtoToEntity(ProfileDTO profileDTO) {
ProfileEntity profileEntity = modelMapper.map(profileDTO, ProfileEntity.class);
return profileEntity;
}

The above function converts DTO into Entity with all the fields that match with Entity class. Sometimes we don't need to accept some fields from UI and process them for save or update. For this, we have to make the field read-only and the field doesn't set up with a setter function. For achieving this Fasterxml Jackson is the right choice.

Fasterxml Jackson is the best Java-based library that is used to serialize the Java objects to JSON and JSON to java objects. It provides many annotations like @JsonSerialize, @JsonCreator, @JsonAnySetter, @JsonSetter, @JsonProperty, @JsonIgnore and etc.

Let me explain the usage of some annotations here.

@JsonSerialize - It is used to serialize the objects. It indicates the custom serializer to use when marshaling (Converts java objects into JSON) entity.

@JsonCreator - This annotation is used to tune the constructor used in de-serialization. We can de-serialize the JSON to an entity that doesn't exactly match the fields.

@JsonAnySetter - This annotation is using Map as standard properties and when de-serialization the properties from JSON will be added to the Map.

@JsonSetter - This annotation is an alternative to @JsonProperty that marks the method as a setter method. This is useful when we need to read some JSON data, but the entity class doesn't exactly match that data, and so we need to do some changes to make it fit.

We will see all other annotations in a separate article. But here for explaining conversion between DTO to Entity I used only two annotations called @JsonProperty and @JsonIgnore.

@JsonProperty(access = Access.READ_ONLY) makes the particular field read-only which means this field we can only fetch from DB. when we pass it from the client the field can't consider it for saving or update. @JsonIgnore is used to make that field null. Here I used ProfileDTO as the example below.

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
public class ProfileDTO {
    private String _id;
    private String firstName;
    private String lastName;
    @JsonProperty(access = Access.READ_ONLY)
    private String userId;

    public String get_id() {
        return _id;
    }
    public void set_id(String _id) {
        this._id = _id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getUserId() {
        return userId;
    }
    @JsonIgnore
    public void setUserId(String userId) {
        this.userId = userId;
    }
}

In the above snippet, the userId field is marked as read-only by using @JsonProperty(access = Access.READ_ONLY) and ignore to set up with setter function by using @JsonIgnore.

So with that above setup if you are passing value for the field userId from UI then it should become null when the conversion happens from DTO to Entity.

I hope you understood how the conversion happens between DTO to Entity. If any concerns please keep me updated.

Leave a Reply

Your email address will not be published. Required fields are marked *