2

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?

1
  • What is dsPersonHits? What type does it have? To match the JSON, it should be a List<Map<String, Object>>. Since you only specified the type as List, it has to be a list of Map, not a list of some POJO. To get a List<MyPojo>, you need to tell the exchange() method that. Commented Jan 18, 2021 at 10:12

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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;

1 Comment

Thanks, I saw another post similar but the example was not clear, this was perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.