In previous post under MapStruct, I showed how to create a custom mapper method and how MapStruct framework calls it. For recap,1) sometimes we cannot depend on the mapping method created by MapStruct. So we create our own custom mapper method which takes care of mapping one object properties to another object properties.2) We instruct…… Continue reading Passing the target type to custom mapper
Category: MapStruct
Calling mapper interface method from another mapper interface code
In this post under MapStruct, I will show with example how to call one mapper interface method from another mapper interface code. For our example I will create “Address” pojo class that will be shared by “Student” and “Person” pojo class as shown below. Address package package31; public class Address { private String addressLine1; private…… Continue reading Calling mapper interface method from another mapper interface code
Using @ObjectFactory annotation to create target objects
In this post under MapStruct, I will explain with example the purpose of “@ObjectFactory” annotation. Till now in all my previous post, I have shown you that MapStruct uses constructor and builder method to create target objects. We can also create an object using factory methods. To instruct MapStruct to stop using constructor, buider method…… Continue reading Using @ObjectFactory annotation to create target objects
Adding custom mapping method to Mapper interface
In this post under MapStruct, I will show with example how to add custom mapping methods if the out of the box generated mapping methods aren’t sufficient. For our example I will have the following pojo classes Person package package29; public class Person { private int id; private String name; private Address address; //Removed getter…… Continue reading Adding custom mapping method to Mapper interface
Mapping beans that refer another bean as its properties
In all my previous posts under MapStruct, whenever I gave an example of MapStruct, I used a bean having only primitive data types and not user defined types. In this post, I will show with example how to do mapping of a bean which refers to another bean as its property. For our example I…… Continue reading Mapping beans that refer another bean as its properties
Using defaultExpressions
In this post under MapStruct, I will explain with example the purpose of “defaultExpressions”. Default expressions are the expressions that are executed with the source attribute is null. I have introduced you to expressions in MapStruct in previous posts. Till now we were using expressions without the source attribute. But in case of default expressions…… Continue reading Using defaultExpressions
Using decorator during mapping
In this post under MapStruct, I will show how to use decorator to implement pre and post mapping operations. For our example, we will use the “Student” and “StudentDTO” pojo classes. The class structure is as shown below Student package package26; public class Student { private int id; private String name; private String className; //Removed…… Continue reading Using decorator during mapping
Using source object in expressions
In this post under MapStruct, I will show with example, how to use source object in our expressions. For our example, lets say we have to map “Student” object to “StudentDTO” object. Below are their class structures Student package package25; public class Student { private int id; private String firstName; private String lastName; //Removed the…… Continue reading Using source object in expressions
Mapping between objects with expressions
In this post under MapStruct, I will show with example how to add expressions in “@Mapping” annotation. For our example we will use the below classes Student package package24; import java.util.Date; public class Student { private int id; private String name; //Removed getter, setter, and toString for brevity } StudentDTO package package24; import java.util.Date; public…… Continue reading Mapping between objects with expressions
Mapping between objects with default and constant date value
In this post under MapStruct, I will show with example how to map a date as default and constant value. Below is the source class Student package package23; import java.util.Date; public class Student { private int id; private String name; //Removed getter, setter, and toString for brevity } The “Student” class has one field named…… Continue reading Mapping between objects with default and constant date value