0

I have a Spring Boot project and I'm using mapstruct to map 2 objects. And I have this king of structure, this is the first object:

ObjectA {
List<ObjectB> objectsB;
}

ObjectB {
String prId;
List<String> dtId;
}

---

The second object:

ObjectC {
List<ObjectD> objectsD;
}

ObjectD {
ObjectE objectE;
List<ObjectE> objectsE;
}

ObjectE {
String nmId;
}

And now using mapstruct I need to do this:

@Mapper(componentModel = "spring")
public interface AppMapper {
    @Mappings({
            @Mapping(target = "objectC.objectsD.objectE.nmId", source = "objectA.objectsB.prId"),
            @Mapping(target = "objectC.objectsD.objectsE.nmId", source = "objectA.objectsB.dtId")
    })
    ObjectC objectAToObjectC(ObjectA objectA);
}

How can I do it? Any feedback will be apreciated! Thank you!

1
  • please show us your findings for the problem that you are facing. You can create a custom mapping to map list by using @AfterMapping Commented Nov 12, 2020 at 16:15

1 Answer 1

3

You have to add custom mapping method to map nested objectB to objectD and any string to ObjectE.

This should work for you given example:

@Mapper
public interface AppMapper
{
    @Mapping(source = "objectsB", target = "objectsD")
    ObjectC objectAToObjectC(ObjectA objectA);

    @Mapping(source = "prId", target = "objectE.nmId")
    @Mapping(source = "dtId", target = "objectsE")
    ObjectD objectBtoObjectD(ObjectB objectB);

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.