使用mapstruct从List <Object>映射List <String>

bhu*_*ana 7 java dto mapstruct

嗨我在使用mapstruct从Child Source类设置它时,在DTO中为List操作获取null.有人可以帮我解决这个问题.请在这里找到我的代码

实体类:

public class Source {
    int id;
    String name;
    List<ChildSource> childSource;
    //getters and setters
}

public class ChildSource {
    String code;
    String action;
    //getters and setters   
}
Run Code Online (Sandbox Code Playgroud)

DestinationDTO:

public class TargetDTO{
    int sNo;
    String mName;
    List<String> actions;
    //getters and setters  
}
Run Code Online (Sandbox Code Playgroud)

MApper类:

@Mapper(componentModel = "spring")    
public abstract class SampleMapper {
        @Mappings({ 
            @Mapping(target = "id", source = "sno"),
            @Mapping(target = "name", source = "mNAme")
        })
        public abstract TargetDTO toDto(Source source);

        @IterableMapping(elementTargetType = String.class)
        protected abstract List<String> mapStringtoList(List<ChildSource> childSource);

        protected String mapChildSourceToString(ChildSource child) {
            return child.getAction();
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是我的动作列表在目标dto中设置为null.请问有人帮我吗?

kin*_*_ru 6

你可以这样做。


@Mapper(componentModel = "spring")    
public abstract class SampleMapper {
        @Mappings({ 
            @Mapping(target = "id", source = "sno"),
            @Mapping(target = "name", source = "mNAme"),
            @Mapping(target = "actions", source = "childSource")
        })
        public abstract TargetDTO toDto(Source source);

        protected abstract List mapStringtoList(List childSource);

        protected String mapChildSourceToString(ChildSource child) {
            return child.getAction();
        }
    }
Run Code Online (Sandbox Code Playgroud)