Simple Object Mapping Example

In this post under MapStruct, I will show with simple example how to map one object attribute values to another object attributes.

For our example I will have two classes as shown below

Student

package package1;

public class Student {
    private int id;
    private String name;
    private String className;

    /*getter and setter methods for each attribute 
    not shown for brevity purposes*/

    @Override
    public String toString() {
        ...
    }
}

StudentDTO

package package1;

public class StudentDTO {
    private int id;
    private String name;
    private String className;

    /*getter and setter methods for each attribute 
    not shown for brevity purposes*/

    @Override
    public String toString() {
        ...
    }
}

For this example I am making sure that attribute names and number of attributes in both the classes are same.

In upcoming blogs I will cover what to do if the attributes differ.

Now the requirement is that I have to copy the attribute values of “Student” class instance to the attributes of “StudentDTO” class instance.

For that I will create an interface which will do the work of mapping attributes from “Student” class instance to “StudentDTO” class instance as shown below.

We call it as Mapper interface.

Mapper interface

package package1;

import org.mapstruct.Mapper;

@Mapper
public interface StudentMapper {
    StudentDTO getDTOFromModel(Student student);
}

As you can see in the above code, We have marked the interface “StudentMapper” with annotation “@Mapper”.

What will happen is, during compilation, whenever MapStruct framework finds the “@Mapper” annotation on an interface, it is an indication to the framework to generate a class that implements this interface and provides an implementation of “getDTOFromModel” method.

So at compile time, MapStruct will generate a class that implements “StudentMapper” interface as shown below

class StudentMapperImpl implements StudentMapper {
    StudentDTO getDTOFromModel(Student student) {
        StudentDTO studentDTO = new StudentDTO();
        studentDTO.setName(student.getName());
        /*same thing for other attributes*/
    }
}

As you can see from the above generated code, inside the method an instance of “StudentDTO” class is created. Then by calling appropriate getter on the source object the value is retrieved, then attributes on the destination object are set with the values by calling corresponding setter methods.

MapStruct assumes that source class and destination class have attributes with same name and of same type, which in this case is true.

Next I will show the main class, where we actually use the interface and its implementation to map “Student” class instance to “StudentDTO” class instance.

Main class


1  package package1;
2  
3  import org.mapstruct.factory.Mappers;
4  
5  public class Example1 {
6      public static void main(String[] args) {
7          StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class);
8          Student student = new Student();
9          student.setClassName("X");
10         student.setName("John");
11         student.setId(1);
12         StudentDTO studentDTO = studentMapper.getDTOFromModel(student);
13         System.out.println(studentDTO);
14     }
15 }

In the above code, at line 7, I call “Mappers” class static “getMapper” method and pass the class object of the mapper interface, which in this case is “StudentMapper” class.

Internally MapStruct will create an instance of “StudentMapperImpl” class and assign it to “studentMapper” variable which is of type “StudentMapper” interface created earlier.

Then from line 8 to 11 I create an instance of “Student” class and populate it with appropriate values.

At line 12, I call “studentMapper” reference variables “getDTOFromModel” method and pass the “Student” instance created at line 8 as an argument.

Since the “studentMapper” reference variable refer to an instance of “StudentMapperImpl” class, the actual implementation of “getDTOFromModel” method is called.

The “getDTOFromModel” method takes the “student” instance and returns an instance of “StudentDTO” which has values copied from “student” instance.

In this way MapStruct maps the attribute values of one object to another object.

Please note we can configure the MapStruct in many different ways, which I will show in upcoming blogs but what I have explained in this post is the default behavior of MapStruct.

Leave a comment