Java 映射列表<;字符串>;从列表<;对象>;使用mapstruct

Java 映射列表<;字符串>;从列表<;对象>;使用mapstruct,java,dto,mapstruct,Java,Dto,Mapstruct,您好,当使用mapstruct从子源类对DTO中的列表操作进行设置时,我得到了null。能帮我解决这个问题吗。请在这里找到我的代码 实体类: public class Source { int id; String name; List<ChildSource> childSource; //getters and setters } public class ChildSource { String code; String act

您好,当使用mapstruct从子源类对DTO中的列表操作进行设置时,我得到了null。能帮我解决这个问题吗。请在这里找到我的代码

实体类:

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

public class ChildSource {
    String code;
    String action;
    //getters and setters   
}
公共类源代码{
int-id;
字符串名;
列出儿童来源;
//接球手和接球手
}
公共类子源{
字符串代码;
弦作用;
//接球手和接球手
}
目的:

public class TargetDTO{
    int sNo;
    String mName;
    List<String> actions;
    //getters and setters  
}
公共类TargetDTO{
int-sNo;
字符串mName;
列出行动;
//接球手和接球手
}
映射器类:

@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();
        }
    }
@Mapper(componentModel=“spring”)
公共抽象类SampleMapper{
@映射({
@映射(target=“id”,source=“sno”),
@映射(target=“name”,source=“mNAme”)
})
公共摘要目标为toDto(来源);
@IterableMapping(elementTargetType=String.class)
受保护的抽象列表mapStringtoList(列表子源);
受保护字符串mapChildSourceToString(ChildSource子级){
返回child.getAction();
}
}

但是我的操作列表在目标dto中设置为null。有人能帮我吗?

你可以这样做


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

生成的代码看起来如何请您在帖子中解释它是如何回答问题的。
    @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);


        default String mapChildSourceToString(ChildSource child) {
            return child.getAction();
        }
}