使用 spring Mapper 映射 Map 对象 - Java
Use spring Mapper to map object of Map - Java
我在尝试使用 spring 的 Mapper 注释功能时遇到了一些困难。我想使用映射器在 java 中映射一个 Map 对象,如下所示:
主机计数器:
@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;
}
错误:
Can't map property
"java.util.Map>
hostsList" to " HostsCounters hostsList". Consider to
declare/implement a mapping method
只需使用:
@Mapping(source = "hostsList", target = "hostsList")
GeneralResponse toResponse(SevCount sevCount, HostsCounters hostsList, List<RegDAO> Reg);
但我强烈建议您去掉具有不同含义的相同名称:
HostsCounters hostsList -> HostsCounters hostsLists
我在尝试使用 spring 的 Mapper 注释功能时遇到了一些困难。我想使用映射器在 java 中映射一个 Map 对象,如下所示:
主机计数器:
@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;
}
错误:
Can't map property "java.util.Map> hostsList" to " HostsCounters hostsList". Consider to declare/implement a mapping method
只需使用:
@Mapping(source = "hostsList", target = "hostsList")
GeneralResponse toResponse(SevCount sevCount, HostsCounters hostsList, List<RegDAO> Reg);
但我强烈建议您去掉具有不同含义的相同名称:
HostsCounters hostsList -> HostsCounters hostsLists