简体   繁体   中英

class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String

I am fetching records from a Rest API with the following code...

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;

Here are the logs. It appears to be getting the JSON list ok but I am getting the following when I try to get at the json to build a List of DSPerson objects.

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"]>

Received Exception: class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap') java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap') at gov.doj.indisearch.datasource.listener.JmsConsumer.onMessage(JmsConsumer.java:62)

How do I get at the JSON in the response?

You need to serialize the response. There are a number of librarys that can help do this for you, for example Jackson or Gson.

In Jackson you might do:

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

In Gson you might do:

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

This is an old question. Please use ParameterizedTypeReference to encapsulate result if using generic type.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM