Skip to content
Advertisement

How to convert an interface with MapStruct?

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:

public class Group {
    ...

    private IUser user;

    ...
}

Here the DTO:

public GroupDTO {

    private Long idUser;

    private String username;
}

We try this mapping :

@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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement