【问题标题】:Passing Hashmap<String, List<MyModelClass>> from Spring Rest Controller resulting in HashMap<String, List<LinkedHashMap>>从 Spring Rest Controller 传递 Hashmap<String, List<MyModelClass>> 导致 HashMap<String, List<LinkedHashMap>>
【发布时间】:2016-06-28 18:02:44
【问题描述】:

我正在尝试从 Spring Rest Controller 传递 HashMap >。从 Rest Controller 中按预期返回值。但是当客户端接收到返回的对象是导致HashMap>而不是HashMap>。

以下是来自客户端服务方法的调用。

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

我在这里做错了什么。我如何在客户端服务中获取 Map > 而不是 Hashmap >。请指教。谢谢。

【问题讨论】:

    标签: rest spring-mvc


    【解决方案1】:

    来自link 的以下 sn-p 对我有用。

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 2011-12-11
      • 2014-09-15
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多