Spring 映射器与 Java 地图

Spring mapper with Java Map

几个小时以来,我一直在为以下问题苦苦挣扎,但我不知道如何让它发挥作用:

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

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

    TeamResponseDTO toDto(TeamResponse teamResponse);

}

团队响应class:

@Data
@NoArgsConstructor
public class TeamResponse {

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

名称和类型class:

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

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

TeamResponseDTO class:

@Data
@NoArgsConstructor
public class TeamResponseDTO {

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

NameAndTypeDTO class:

@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: 

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


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

    TeamResponseDTO toDto(TeamResponse teamResponse);

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

    NameAndTypeDTO nameAndTypeToDTO(NameAndType nameAndType);

}

此致, 维普