对于使用Java Spring RestTemplate完成的HTTP请求,我获得了带有动态键的JSON键-值对对象的响应,如下所示。
响应:
{ "1234x": { "id": "1234x", "description": "bla bla", ... }, "5678a": { "id": "5678a", "description": "bla bla bla", ... }, ... }
如何将响应对象映射到POJO或Map?
我正在使用RestTemplate如下。
RestTemplate restTemplate = new RestTemplate(); String url = "my url"; HttpHeaders headers = new HttpHeaders(); HttpEntity entity = new HttpEntity(headers); response = restTemplate.exchange(url, HttpMethod.GET, entity, ???);
您可以将 ParameterizedTypeReference 与 Map 一起使用(您可以根据自己的用例进行自定义):
response = restTemplate.exchange(url, HttpMethod.GET, entity, new ParameterizedTypeReference<Map<String, Object>>() {});