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?
dsPersonHits? What type does it have? To match the JSON, it should be aList<Map<String, Object>>. Since you only specified the type asList, it has to be a list ofMap, not a list of some POJO. To get aList<MyPojo>, you need to tell theexchange()method that.