Mapping List<String> to DTO

5,724 views
Skip to first unread message

Timo Gruetter

unread,
Mar 17, 2015, 2:58:49 AM3/17/15
to mapstru...@googlegroups.com
Hello out there,

first I want to thank for this beautiful project. We first used Dozer, but because of some problems I was looking for alternatives and finally found MapStruct - and I really like it!

But at the moment I have a problem: I want to map a simple List<String> to a DTO witha property of type List<String>.

@Mapper
public interface MyMapper {
MyDto mapListToDto(List<String> list);
}

public class MyDto {
List<String> myList;
// + getter and setter
}


Is there a simple way to do so? At the Moment, I get a compile error:
"Can't generate mapping method from iterable type to non-iterable type. "

(Also mentioned at the documentation 6. Mapping collections:
"Note! It is not allowed to declare mapping methods with an iterable source
and a non-iterable target or the other way around. An error will be raised
when detecting this situation.")

How can I get MapStruct to put the List to MyDto ?

Gunnar Morling

unread,
Mar 17, 2015, 7:48:09 AM3/17/15
to mapstru...@googlegroups.com
Hi Timo,

Thanks for the nice feedback. Very glad to hear you like MapStruct!

What you are trying to do is not directly possible as you found out already. Could you describe the actual use case for this and maybe give a "real world" example where it would be useful?

If you really only have a list of strings and the target bean has no further properties, I would simply write a method for that from hand. You e.g. can do so by making your mapper an abstract class and just add that method yourself. Or you "import" it from a hand-written mapper type via @Mapper#uses().

If your target bean actually has more properties besides the string list and these properties are to be populated from another source bean, you could leverage a mapping method with several source objects:

    CustomerDto customerAndPhoneListToDto(Customer customer, List<String> phoneNumbers);

This would populate all the CustomerDto properties with the corresponding properties from the given Customer object and only the "phoneNumbers" property would be populated with the given list. If the "customer" object itself had a list of phone numbers already but you wanted to propagate the separately given list instead, you could do so like this:

    @Mapping(target="phoneNumbers", source="extraPhoneNumbers")
    CustomerDto customerAndPhoneListToDto(Customer customer, List<String> extraPhoneNumbers);

Hth,

--Gunnar

Timo Gruetter

unread,
Mar 17, 2015, 3:51:11 PM3/17/15
to mapstru...@googlegroups.com
Hi Gunnar,

thanks for the very fast reply.

I have already tested the manual way, just wanted to know where there is easier way to do so.

The scenario is quiet simple:
I have a generated CXF Webservice Client, and CXF puts all Params of a Method in one Object (also if it is just one List<String>).

 myService(RequestParamWrapper) ;

I have to write a Wrapper for our service, but I don't want my clients to use the generated objects. Instead I get the List<String> directly and want to auto-map it to the RequestParamWrapper.

Rahul Rao

unread,
Apr 15, 2019, 7:24:09 PM4/15/19
to mapstruct-users
Hi,

I have this scenario now where I have a list of ApplicationDTO's and I want to map it to an ApplicationList.

ApplicationList mapApplicationListToList(List<ApplicationDTO> applicationDTOList);

The ApplicationList consists of 2 properties:

private List<Application> items = new ArrayList<Application>();
private List<LinkDescription> links = new ArrayList<LinkDescription>();

I want to map ApplicationDTO list to the `items` attribute in ApplicationList. Is this possible now using MapStruct?

Apologies if this is already answered. Asking as it's been nearly 4 years since this reply.

Thanks,
Rahul
Reply all
Reply to author
Forward
0 new messages