I made some research in the mapStruct documentation but did not find or understood what I could do to achieve this: the conversation of an entity containing an interface to a flat DTO object.
Here the entity with the attribute:
JavaScript
x
public class Group {
private IUser user;
}
Here the DTO:
JavaScript
public GroupDTO {
private Long idUser;
private String username;
}
We try this mapping :
JavaScript
@Mappings({
@Mapping(source = "user.id", target = "idUser"),
@Mapping(source = "user.name", target = "username")})
GroupDTO toDTO(Group entity);
With a classic mapping, mapStruct generates an error (IUser is abstract; cannot be instantiated).
Any idea ? Thank you.
Advertisement
Answer
Not sure its the best way to do it…
But we used @AfterMapping to create manually in default method the object to implement on interface.
We had to delete @InheritInverseConfiguration for reverse operation.