java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class com.springboot.Employeeportal.dto.EmployeeDto

698 Views Asked by At

I am using Elastic Search Request object to perform a Get request by passing a SQL query. The EntityUtils.toString(response.getEntity()) returns a String with following values,

{
"columns":
[
{
"name":"EmployeeId",
"type":"long"
},
{
"name":"Salary",
"type":"long"
} 
],
"rows":
[[
12345 , 30000
]]
} 

As per our usecase, The above rows section will hold only one record in the arraylist. I have a Dto class as follow,

@Data
public class EmployeeDto{

    public EmployeeDto(EmployeeDto employeeDto) {
        
        // TODO Auto-generated constructor stub
    }
    private Long EmployeeId;
    private Long Salary;
}

I need to retrieve record from the rows section in the response and cast it to the above dto so that I can retrieve values and use it for other logic.

I tried the following,

import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
....
....
Response response = restClient.performRequest(request);
InputStream is = response.getEntity().getContent();
final ObjectMapper mapper = new ObjectMapper();
final Map<String, Object> map = mapper.readValue(is, Map.class);
ArrayList<EmployeeDto> rowsList = (ArrayList<EmployeeDto>) map.get("rows");
Long salaryValue = rowsList .get(0).getSalary();

Upon trying the above , I am facing the follow error ,

java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class com.springboot.Employeeportal.dto.EmployeeDto(java.util.ArrayList is in module java.base of loader 'bootstrap'; com.springboot.Employeeportal.dto.EmployeeDto is in unnamed module of loader 'app')

Kindly advise on how to retrieve the value from rows in the json and cast it to the Emoployee object. It would be really helpful if anyone could help on this issue. Have been stuck with this issue for a while. Thanks!

0

There are 0 best solutions below