Map custom method mapper to Mapstruct

We Are Going To Discuss About Map custom method mapper to Mapstruct. So lets Start this Java Article.

Map custom method mapper to Mapstruct

Advertisements
  1. Map custom method mapper to Mapstruct

    As you have multiple default methods that return the same type. You would need to use Mapping method selection based on qualifiers.
    What this means is that you would need to write your mapper in the following format:
    @Mapper public interface ItemMapper {

  2. Map custom method mapper to Mapstruct

    As you have multiple default methods that return the same type. You would need to use Mapping method selection based on qualifiers.
    What this means is that you would need to write your mapper in the following format:
    @Mapper public interface ItemMapper {

Solution 1

Advertisements

As you have multiple default methods that return the same type. You would need to use Mapping method selection based on qualifiers.

What this means is that you would need to write your mapper in the following format:

@Mapper
public interface ItemMapper {

    // Omitting other mappings for clarity
    @Mapping(source = "item", target = "locationDto", qualifiedByName = "locationDto")
    @Mapping(source = "item", target = "binType", qualifiedByName = "binType")
    @Mapping(source = "item", target = "lotSize", qualifiedByName = "lotSize")
    @Mapping(source = "item", target = "stockRails", qualifiedByName = "stockRails")
    ItemViewModel itemToDto(Item item);

    @Named("locationDto")
    default String locationToLocationDto(Item item) {
        //Omitting implementation
    }

    @Named("binType")
    default double locationToBinType(Item item) {
        //Omitting implementation
    }

    @Named("lotSize")
    default double itemToLotsize(Item item) {
        //Omitting implementation
    }

    @Named("stockRails")
    default double stockRails(Item item) {
        //Omitting implementation
    }
}

Some important notes:

  • You need to use @Named from the MapStruct package
  • In source you can also specify the name of the parameter of the method
  • In qualifiedByName you need to specify the value that you have written in @Named

I would strongly advise against using expressions for such complicated things. It is much more difficult to get it correct and it is more difficult for maintaining

Original Author Filip Of This Content

Solution 2

Advertisements

Simplest way is using powerfull mapstruct @AfterMapping annotation. E.g.

@AfterMapping
public void treatAdditional(User user, @MappingTarget StudentSkillsTo studentSkillsTo) {
    System.out.println("After mapping!");
}

Original Author Grigory Kislin Of This Content

Solution 3

Advertisements

Mapstruct can use similar constructions:

@Mapping(target = "name", expression = "java(user.getName() != null " +
        " ? user.getName() : "DefaultName")")

an expression can include any constructions on java
e.g

 item.getItemsOnDetailedLocations()
.iterator().next().getLocation().getLocation();

if the method is large, then it is worthwhile to take it to another service and call this way

Original Author Redbore Of This Content

Solution 4

Advertisements

You can simply use them like following

@Mapping( target="/*Enter targetFieldName*/", expression="java( /default method which calculates target field/" )

Original Author Naveen Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment