Java Map的Spring映射器

时间:2019-05-19 14:41:33

标签: java spring lombok mapper

我已经为以下问题苦苦挣扎了几个小时,但我不知道该如何使它起作用:

Spring映射器,以便将数据库响应转换为DTO:

@Mapper(componentModel = "spring")
public interface ITeamResponseToDtoMapper {

    TeamResponseDTO toDto(TeamResponse teamResponse);

}

TeamResponse类:

@Data
@NoArgsConstructor
public class TeamResponse {

    private Map<String, List<NameAndType>> teamList;
}

NameAndType类:

@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class NameAndType{

    private String name;
    private String type;
    private String team;
}

TeamResponseDTO类:

@Data
@NoArgsConstructor
public class TeamResponseDTO {

    private Map<String, List<NameAndTypeDTO >> teamList;
}

NameAndTypeDTO类:

@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PUBLIC)
public class NameAndTypeDTO {

    private String name;
    private String type;
    private String team;
}

基本上,“ NameAndType”和“ NameAndTypeDTO”是相同的,为什么转换失败?

error: Can't map property "java.util.Map<java.lang.String,java.util.List<com.microservices.teamservice.dataobjects.NameAndType>> teamList" to "java.util.Map<java.lang.String,java.util.List<com.microservices.teamservice.api.dataobjects.NameAndTypeDTO>> teamList". Consider to declare/implement a mapping method: 

1 个答案:

答案 0 :(得分:1)

我认为您需要显式添加方法来映射整个类链。在您的示例中,以下应该起作用:


@Mapper(componentModel = "spring")
public interface ITeamResponseToDtoMapper {

    TeamResponseDTO toDto(TeamResponse teamResponse);

    List<NameAndTypeDTO> natListToDTO(List<NameAndType> natList);

    NameAndTypeDTO nameAndTypeToDTO(NameAndType nameAndType);

}

致谢, WiPu