class java.lang.Long cannot be cast to class java.lang.String error when having a hashmap of mixed types (Long and string)

I have spring boot – camel application that connects to a postgre database and returms from a select query the following array:

[{id=1, name=JENNY, country=LB}, {id=2, name=AMIGOSCODE, country=UK}]

id is of type bigint in the database however name is of type string

when executing the following code:

@Component
public class InsertRestService extends RouteBuilder {

  

    @Override
    public void configure() throws Exception {
        

        
        rest("/").produces("application.json")
        .get("selectPerson/{name}")//.outType(EmployeeInfo.class)
        .to("direct:selectPerson");

    from("direct:selectPerson")
        .transform().simple("SELECT * FROM person WHERE name=" + "'${header.name}'")
        .log("${body}")
        .to("jdbc:dataSource") //spring boot starter jdbc creates the bean in the registry
        .process(new Processor() {
            public void process(Exchange xchg) throws Exception {
               //the camel jdbc select query has been executed. We get the list of employees.
                ArrayList<HashMap<String, String>> dataList = (ArrayList<HashMap<String, String>>) xchg.getIn().getBody();
                        
                List<EmployeeInfo> employees = new ArrayList<EmployeeInfo>();
                System.out.println(dataList);
                for (HashMap<String, String> data : dataList) {
                    EmployeeInfo employee = new EmployeeInfo();
                    
                    employee.setEmpId(data.get("id"));
                    
                    employee.setEmpName(data.get("name"));
                    
                    employees.add(employee);
                    
                }
                xchg.getIn().setBody(employees)
                ;
            }
        
        })
        .marshal().json(JsonLibrary.Gson)
        .log("${body}");
        
        
     
    }
}

I am getting the following error:

java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.String (java.lang.Long and java.lang.String are in module java.base of loader 'bootstrap')

I think it is because of the hashmap HashMap<String, String> having the types string but I don t know how to do it since I have mixed types in hashMap.

Also I have a class:

public class EmployeeInfo {

    private String empId;
    private String empName;

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    @Override
    public String toString() {
        return "[empId=" + empId + ", empName=" + empName + "]";
    }

}

when trying to change the type of empId to “long“` it does not work as well.

I would appreciate any help! Thanks in advance!

Answer

Did you try converting

ArrayList<HashMap<String, String>>

to

ArrayList<HashMap<String, Object>>

Post that based on your field name when you do data.get() you can type cast it to required type by instance checking.

Also, if you want to check types: you can add a log before each field access

Object obj = data.get(<field>);
log(obj.getClass().getName());

Hope this is helpful.