通用列表对象映射器

huangapple go评论79阅读模式
英文:

Generic List object mapper

问题

我正在尝试从对象映射器获取通用列表,但它没有返回特定类型的列表。

public <T, V> List<V> invokeService(T request, String url, Class<V> type) {

    HttpEntity requestEntity = new HttpEntity<>(sdsRequest, headers);
    ResponseEntity<String> response = restTemplate.exchange(returnURI, HttpMethod.POST, requestEntity, String.class);

    ObjectMapper objectMapper = new ObjectMapper();
    ObjectReader objectReader = objectMapper.reader().forType(new TypeReference<List<V>>() {
    });
    try {
        List<V> list = objectReader.readValue(response.getBody());
        log.info("returned value {}", list);
        return list;
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}

输出

返回值为[{settlement_group_id=72, id=3142193, value=812954, code=GO}]

如我们在输出中所见,它被转换为列表,但不是特定类型的列表。

异常

java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.service.dtos.PartyMapResponseDTO (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.service.dtos.PartyMapResponseDTO is in unnamed module of loader 'app')

英文:

I am trying to get a generic list from object mapper but it not retunning a type specific list.

    public &lt;T,V&gt; List&lt;V&gt; invokeService(T request, String url,Class&lt;V&gt; type) {

    HttpEntity requestEntity = new HttpEntity&lt;&gt;(sdsRequest, headers);
      ResponseEntity&lt;String&gt;  response= restTemplate.exchange(returnURI,HttpMethod.POST,requestEntity,String.class);

  ObjectMapper objectMapper=new ObjectMapper();
  ObjectReader objectReader = objectMapper.reader().forType(new TypeReference&lt;List&lt;V&gt;&gt;(){});
  try {
    List&lt;V&gt; list = objectReader.readValue(response.getBody());
    log.info(&quot;returned value {}&quot;,list );
    return list;
  } catch (JsonProcessingException e) {
    throw new RuntimeException(e);
  }
}

Outout

returned value [{settlement_group_id=72, id=3142193, value=812954, code=GO}]

As we can see in the output. It is getting converted to List but not of a particular type.

Exception:

java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.service.dtos.PartyMapResponseDTO (java.util.LinkedHashMap is in module java.base of loader &#39;bootstrap&#39;; com.service.dtos.PartyMapResponseDTO is in unnamed module of loader &#39;app&#39;)

答案1

得分: 0

已解决此问题:

private <T> List<T> getObjectList(final String json, final Class<T> cls) throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper
            .readValue(json, objectMapper.getTypeFactory()
                    .constructCollectionType(ArrayList.class, cls));
}

List<V> objectList = getObjectList(response.getBody(), type);
log.info("返回的对象新方法 {}", objectList);

日志:

返回的对象新方法 [PartyMapResponseDTO(id=31193, value=80954, settlement_group_id=72, code=GOL)]
英文:

Solved this:

 private &lt;T&gt; List&lt;T&gt; getObjectList(final String json, final Class&lt;T&gt; cls) throws JsonProcessingException {
    ObjectMapper objectMapper=new ObjectMapper();
    return objectMapper
            .readValue(json, objectMapper.getTypeFactory()
                    .constructCollectionType(ArrayList.class, cls));
  }

List&lt;V&gt; objectList = getObjectList(response.getBody(), type);
log.info(&quot;returned object new method {}&quot;, objectList);

Log:

 returned object new method [PartyMapResponseDTO(id=31193, value=80954, settlement_group_id=72, code=GOL)]

huangapple
  • 本文由 发表于 2023年7月13日 00:53:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定