Reputation: 2013
I have a list of string in my DTO, i want to map it into a list of object, in the mapper i used the service to get the object by this string, but i have the below error
Can't map property "java.util.List<java.lang.String> customers" to "java.util.List<com.softilys.soyouz.domain.Customer> customers".
Consider to declare/implement a mapping method: "java.util.List<com.softilys.soyouz.domain.Customer> map(java.util.List<java.lang.String> value)".
public class FirstDomain implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
private String description;
private List<Customer> customers;
}
public class FirstDomainDTO {
private String id;
private String description;
private List<String> customers;
}
@Mapper(uses = { CustomerService.class })
public interface FirstDomainMapper extends EntityMapper<FirstDomainDTO, FirstDomain> {
@Mapping(source = "customers", target = "customers")
FirstDomainDTO toDto(FirstDomain firstDomain);
@Mapping(source = "customers", target = "customers")
FirstDomain toEntity(FirstDomainDTO firstDomainDTO);
default String fromCustomer(Customer customer) {
return customer == null ? null : customer.getCode();
}
}
Upvotes: 2
Views: 4768
Reputation: 21393
The error message you are getting should be enough to help you understand what the problem is. In this case MapStruct doesn't know how to map from List<String>
into List<Customer>
. The other way around is OK since you have defined
default String fromCustomer(Customer customer) {
return customer == null ? null : customer.getCode();
}
To fix this you need to defined the reverse as well.
@Mapper(uses = { CustomerService.class })
public interface FirstDomainMapper extends EntityMapper<FirstDomainDTO, FirstDomain> {
@Mapping(source = "customers", target = "customers")
FirstDomainDTO toDto(FirstDomain firstDomain);
@Mapping(source = "customers", target = "customers")
FirstDomain toEntity(FirstDomainDTO firstDomainDTO);
default String fromCustomer(Customer customer) {
return customer == null ? null : customer.getCode();
}
default Customer fromStringToCustomer(String customerId) {
// Implement your custom mapping logic here
}
}
Upvotes: 3