Spring RestTemplate and generic types ParameterizedTypeReference collections like List

前端 未结 6 678
一生所求
一生所求 2020-12-05 09:44

An Abstract controller class requires List of objects from REST. While using Spring RestTemplate its not mapping it to required class instead it returns Linked HashMAp

6条回答
  •  感情败类
    2020-12-05 10:25

    Couldnt find a solution from Spring, hence I have done it with ParameterizedTypeReference in HashMap like

     public final static HashMap paramTypeRefMap = new HashMap() ;
     static {
        paramTypeRefMap.put(AttributeDefinition.class, new ParameterizedTypeReference>(){} );
        paramTypeRefMap.put(AttributeInfo.class, new ParameterizedTypeReference>(){} );
     }
    

    and used it

    ParameterizedTypeReference parameterizedTypeReference = paramTypeRefMap.get(requiredClass);
    ResponseEntity exchange = restTemplate.exchange(uri, HttpMethod.POST, entity, parameterizedTypeReference);
    

提交回复
热议问题