英文:
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 <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);
}
}
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 'bootstrap'; com.service.dtos.PartyMapResponseDTO is in unnamed module of loader 'app')
答案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 <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("returned object new method {}", objectList);
Log:
returned object new method [PartyMapResponseDTO(id=31193, value=80954, settlement_group_id=72, code=GOL)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论