【发布时间】:2018-12-19 23:26:44
【问题描述】:
我有一个 Java 映射,我想使用 DTO 进行映射作为响应。我试过这个:
服务:
@Service
public class GatewaysService {
public Map<Integer, String> getGatewaysList() {
Map<Integer, String> list = new HashMap<>();
list.put(1, "Bogus");
return list;
}
}
API:
@Autowired
private GatewaysService gatewaysService;
@GetMapping("gateways")
public ResponseEntity<?> getGateways() {
return ok(gatewaysService.getGatewaysList().map(mapper::toGatewayMap));
}
DTO:
public class ContractGatewaysDTO {
private Integer id;
private String gateway;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGateway() {
return gateway;
}
public void setGateway(String gateway) {
this.gateway = gateway;
}
}
映射器:
Map<Integer, String> toGatewayMap(ContractGatewaysDTO dto);
但我得到错误:
The method map(ContractDTO) in the type ContractsMapper is not applicable for the arguments (mapper::toGatewayMap)
映射它的正确方法是什么?我想将 Java 映射转换为键值响应。
【问题讨论】:
-
Map什么时候有
map方法? -
我想将 Java 映射转换为键值响应。 但映射正是键值。直接退货吧。
-
我同意,但另一端 - angular 需要知道密钥。
-
角度与它有什么关系。客户在这里无关紧要。
标签: java spring spring-boot spring-restcontroller spring-rest