Reputation: 999
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.
Upvotes: 1
Views: 4277
Reputation: 488
hi i am using map struct version : 1.3.1.Final and i mapped from interface to dto easily:
@Mapper(componentModel = "spring")
public interface OrderSummeryMapper {
AllocatedOrderSummeryResponse toOrderSummery (AllocatedOrderSummery orderSummery);
}
my dto:
@Data //lombok annotation
@NoArgsConstructor
@AllArgsConstructor
public class AllocatedOrderSummeryResponse implements Serializable {
Integer customerId;
String deliveryCategory;
}
interface:
public interface AllocatedOrderSummery {
Integer getCustomerId();
String getDeliveryCategory();
}
Upvotes: 1
Reputation: 18990
It should work with an object factory method:
public IUser createIUser() {
return new User();
}
This factory method will the be invoked if an instance of IUser
is needed.
Upvotes: 0
Reputation: 999
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.
Upvotes: 1