0

I get a response of JSON key-value pair object with with dynamic keys for a HTTP request done using Java Spring RestTemplate as shown below.

Response:

{ "1234x": { "id": "1234x", "description": "bla bla", ... }, "5678a": { "id": "5678a", "description": "bla bla bla", ... }, ... } 

How to map the response object to a POJO or a Map ?

I am using RestTemplate as following.

RestTemplate restTemplate = new RestTemplate(); String url = "my url"; HttpHeaders headers = new HttpHeaders(); HttpEntity entity = new HttpEntity(headers); response = restTemplate.exchange(url, HttpMethod.GET, entity, ???); 
4
  • Get response as String and Parse it using JSON. Commented Nov 27, 2018 at 6:50
  • 1
    Second approch is like : this link below stackoverflow.com/questions/35465273/… Commented Nov 27, 2018 at 6:52
  • Does json has same structure for all objects? How dynamic it is, parameter names or length changes? Commented Nov 27, 2018 at 6:58
  • @Emre Savcı as you see keys are the ids. So its different everytime and number of key-value pairs also differ. Commented Nov 27, 2018 at 7:06

2 Answers 2

4

You can simply use ParameterizedTypeReference with Map (you can customize it according to your use case) :

response = restTemplate.exchange(url, HttpMethod.GET, entity, new ParameterizedTypeReference<Map<String, Object>>() {}); 
Sign up to request clarification or add additional context in comments.

7 Comments

When I used this there is an error in unit test. Mockito.when(restTemplate.exchange(......)).thenReturn(new ResponseEntity<HashMap<String, ItemDetails>>(map, HttpStatus.ACCEPTED));. It says cannot resolve method thenReturn(...) with the parameter type.
Can you share full exception? It seems like it related with your return type passed to thenReturn method.
It is a syntax error Cannot resolve method 'thenReturn(org.springframework.http.ResponseEntity<java.util.HashMap<java.lang.String,com.myproject.MyClass>>)'
You should give some value inside thenReturn() method like thenReturn(ResponseEntity.ok(new HashMap())); .
I gave a value. thenReturn(new ResponseEntity<HashMap<String, ItemDetails>>(map, HttpStatus.ACCEPTED));
|
1

You could use the new ObjectMapper.readValue() and specify TypeReference as new TypeReference<Map<String, SimplePOJO>>() {});

public static void main(String[] args) throws IOException { final String json = "{\"1234x\": {\"id\": \"1234x\", \"description\": \"bla bla\"}, \"5678a\": {\"id\": \"5678a\", \"description\": \"bla bla bla\"}}"; Map<String, SimplePOJO> deserialize = new ObjectMapper().readValue(json, new TypeReference<Map<String, SimplePOJO>>() {}); } public static class SimplePOJO { private String id; private String description; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SimplePOJO that = (SimplePOJO) o; return Objects.equals(id, that.id) && Objects.equals(description, that.description); } @Override public int hashCode() { return Objects.hash(id, description); } } 

2 Comments

How to combine this with restTemplate.exchange() method?
@ShanikaEdiriweera response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); Map<String, SimplePOJO> deserialize = new ObjectMapper().readValue(response.getBody(), new TypeReference<Map<String, SimplePOJO>>() {});

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.