Use Mapper in Another Mapper with MapStruct and Java

06 May 2023 Balmiki Mandal 0 Core Java

Using MapStruct with Java to Power Another Mapper

MapStruct is a powerful library that facilitates the mapping of complex objects across different frameworks and technologies. Many developers know and love MapStruct for its ease of use and flexibility when dealing with mapping between source and target objects. But what if you need to map one type of object to another type of object that isn’t supported by MapStruct? One approach is to use another mapper, such as Dozer or ModelMapper, but this introduces an extra layer of complexity and can be time consuming. Wouldn’t it be great if you could just use MapStruct? Good news — you can! With Java and MapStruct, you can create a mapper to read from any source and write to any target object. The key is to create two new types of classes: one for reading from the source, and one for writing to the target. You then map between these two types using MapStruct. Let’s take a look at how this works. First, create two new classes. For example, let’s say you want to map a Person class to a Customer class. You can create a PersonDto class and a CustomerDto class. Next, create the appropriate mapper classes for each object. For example, for the PersonDto, you would have a PersonMapper class, and for the CustomerDto, you would have a CustomerMapper class. Now, you’re ready to map between the two types. Using MapStruct, you can write a method in your PersonMapper class that takes a PersonDto as an argument, maps it to a CustomerDto, and returns the mapped CustomerDto. To do this, you would use the @Mapping annotation, like so: @Mapping(source = “personDto”, target = “customerDto”) public CustomerDto map(PersonDto personDto) { // Code to map between PersonDto and CustomerDto } This method will be called when mapping between Person and Customer objects, and it will automatically map from one to the other based on the annotations. Once you’ve defined the mapping methods, you’re ready to use MapStruct to map between the two objects. You can use a method call between the two mappers to do the actual mapping. For example, you could use the following code: PersonMapper.map(personDto); CustomerDto customerDto= CustomerMapper.map(personDto); Now, whenever you need to map between Person objects and Customer objects, you can just call the PersonMapper and CustomerMapper methods instead of having to write out the mapping code yourself. This saves you time and makes your code much cleaner and more readable. As you can see, using MapStruct with Java allows you to create powerful mappers that can map between any two types of objects. This is especially useful if you’re dealing with complex data structures, or if you need to read from multiple sources and write to multiple targets. Give it a try and see how much easier your life can be!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.