Object Mapping between classes with different attributes

In the previous post under MapStruct I showed an example of how to use MapStruct framework for object mapping. I assumed that attribute names in source class and destination class are same.

What if they are not.

In this post, I will show with an example how to configure MapStruct to map between source and destination class when there attribute names don’t match.

For our example I will use the below two data class

Student

package package2;

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

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

StudentDTO

package package2;

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

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

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

When you compare the two data classes, two attributes name match but the third attribute (“className” in “Student” and “classVal” in “StudentDTO”) doesn’t match.

If we go with MapStruct default behavior, the attribute “className” value is not mapped to attribute “classVal”. You will get a warning and value of the attribute “classVal” is set to null.

To fix this, we will create “StudentMapper” interface in the same way we did in previous post but with a small change.

The below code snippet shows “StudentMapper” interface with default configuration explained in previous post.

Previous code snippet

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

The below code snippet shows same “StudentMapper” interface with the change we have to do to fix the above problem

New code snippet

@Mapper
public interface StudentMapper {
    @Mapping(target = "classVal", source = "className")
    StudentDTO getDTOFromModel(Student student);
}

If we compare both snippet the only difference is addition of “@Mapping” annotation at the method level.

We have to take the help of “@Mapping” to tell the MapStruct to map the value of attribute “className” from source class to attribute “classVal” in destination class.

We have given two arguments to this annotation “target” and “source”. The “target” attribute will have name of attribute on the target class and “source” attribute will have the name of attribute on
the source class.

We don’t have to mention the target and source class in “@Mapping” annotation. This will be inferred by MapStruct from the method signature.

In this way we can map two classes who have different attribute names.

Below is the complete main class for your reference

Main class

package package2;

import org.mapstruct.factory.Mappers;

public class Example2 {
    public static void main(String[] args) {
        StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setClassName("X");
        student.setName("John");
        student.setId(1);
        StudentDTO studentDTO = studentMapper.getDTOFromModel(student);
        System.out.println(studentDTO);
    }
}

Leave a comment