1

I'm trying to pass HashMap < String, List< MyModelClass>> from Spring Rest Controller. From the Rest Controller the values are returned as expected. But when the client receives the returned object is resulting in HashMap < String, List< LinkedHashMap>> instead HashMap < String, List< MyModelClass>>.

Below is the call from client service method.

Map<String, List<MyModelClass>> map = new HashMap<String, List<MyModelClass>> ();
map = (Map<String, List<MyModelClass>>)restTemplate.getForObject(restURL + "/someMethod/"+parameter, HashMap.class);

What am I doing wrong here. How would I get the Map < String, List< MyModelClass>> in client service instead of Hashmap < String, List< LinkedHashMap>>. Please advise. Thanks.

gopal
  • 63
  • 1
  • 9

1 Answers1

0

The below snippet from this link worked for me.

ParameterizedTypeReference<List<MyModelClass>> typeRef 
    = new ParameterizedTypeReference<List<MyModelClass>>() {};

ResponseEntity<List<MyModelClass>> responseEntity 
    = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(mvm), typeRef);

List<MyModelClass> myModelClasses = responseEntity.getBody();
Valentin Genevrais
  • 421
  • 1
  • 6
  • 21
gopal
  • 63
  • 1
  • 9