类java.util.linkedhashmap不能转换为类java.lang.string(java.util.linkedhashmap和java.lang.string

mi7gmzs6  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(505)

我正在用下面的代码从RESTAPI获取记录。。。

HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(bimap, headers);
    ResponseEntity<List> response = null;
    try {
        response = restTemplate.exchange("http://localhost:8080/dsperson/getallmatching",
                                  HttpMethod.POST,
                                  entity,
                                  List.class);
        dsPersonHits = response.getBody();
    } catch (RestClientException e) {
        // TODO Auto-generated catch block
        System.out.println("getMatches exception:" + e.getLocalizedMessage());
        e.printStackTrace();
    }
    System.out.println("getMatches response:" + response);
    return dsPersonHits;

这是日志。它看起来得到了json列表,但是当我试图获得json来构建dsperson对象列表时,我得到了以下内容。

getMatches response:<200,[{id=1, userName=, title=Mr., firstName=Abel, middleName=, lastName=Baker, suffix=, alias=HITMAN , birthday=, philSysNumber=PSN:12345, dataSource=BI, criminalCaseNumber=, caseDescription=invalid visa, caseStatus=Settled}, {id=2, userName=, title=Ms., firstName=Alice, middleName=, lastName=Baker, suffix=, alias=Alice , birthday=, philSysNumber=PSN:12346, dataSource=BI, criminalCaseNumber=, caseDescription=invalid visa, caseStatus=Settled}, {id=3, userName=, title=Mr., firstName=Abrahim, middleName=, lastName=Balous, suffix=, alias=Abe, birthday=, philSysNumber=, dataSource=DOJ, criminalCaseNumber=CCN:123456, caseDescription=Murder, caseStatus=Convicted}],[Content-Type:"application/json", Transfer-Encoding:"chunked", Date:"Mon, 18 Jan 2021 09:30:26 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>

收到异常:类java.util.linkedhashmap不能转换为类java.lang.string(java.util.linkedhashmap和java.lang.string在加载程序“bootstrap”的模块java.base中)java.lang.classcastexception:类java.util.linkedhashmap不能转换为类java.lang.string(java.util.linkedhashmap和java.lang.string在模块中)位于gov.doj.indearch.datasource.listener.jmsconsumer.onmessage(jmsconsumer。java:62)
如何获取响应中的json?

d4so4syb

d4so4syb1#

这是个老问题。如果使用泛型类型,请使用ParameteredTypeReference封装结果。

import org.springframework.core.ParameterizedTypeReference;

ParameterizedTypeReference<List<DSPerson>> ptr =
            new ParameterizedTypeReference<List<DSPerson>>() {};
List<DSPerson> dsPersonHits = null;

HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(bimap, headers);
    ResponseEntity<List<DSPerson>> response = null;
    try {
        response = restTemplate.exchange("http://localhost:8080/dsperson/getallmatching",
                                  HttpMethod.POST,
                                  entity,
                                  ptr);
        dsPersonHits = response.getBody();
    } catch (RestClientException e) {
        // TODO Auto-generated catch block
        System.out.println("getMatches exception:" + e.getLocalizedMessage());
        e.printStackTrace();
    }
    //System.out.println("getMatches response:" + response);
    return dsPersonHits;
r7xajy2e

r7xajy2e2#

您需要序列化响应。有很多图书馆可以帮你做到这一点,例如Jackson或格森。
在Jackson,你可能会:

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(serializableObject);

在gson中,您可以执行以下操作:

Gson gson = new Gson();
String json = gson.toJson(serializableObject);

相关问题