2

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();
    }

}
1
  • Try looking into qualifiedByName or qualifiedByClass as a parameter in your @Mapping annotations to do your conversion. Commented Dec 6, 2019 at 15:00

1 Answer 1

3

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