使用Spring Mapper映射Map对象-Java

时间:2019-04-09 10:32:09

标签: java spring jpa mapper

在尝试使用spring的Mapper注释功能时,我遇到了一些困难。我想使用映射器按以下方式映射Java中的Map对象:

HostCounters:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class HostsCounters {
    private Map<String,Map<String,Long>> hostsList;
}

映射器:

@Mapper(componentModel = "spring")
public interface ResponseMapper {
    GeneralResponse toResponse(SevCount sevCount, HostsCounters hostsList, List<RegDAO> Reg);
}

常规响应:

@Data
@NoArgsConstructor
public class GeneralResponse {
    private List<RegDAO> Reg;
    private HostsCounters hostsList;
    private SevCount sevCount;
}    

错误:

  

无法映射媒体资源   “ java.util.Map>   hostsList“到” HostsCounters hostsList“。考虑到   声明/实现映射方法

1 个答案:

答案 0 :(得分:1)

只需使用:

@Mapping(source = "hostsList", target = "hostsList")
GeneralResponse toResponse(SevCount sevCount, HostsCounters hostsList, List<RegDAO> Reg);

但是我强烈建议您摆脱具有不同含义的相同名称:

HostsCounters hostsList -> HostsCounters hostsLists