我有 spring boot-camel 應用程式,它連接到 postgre 資料庫并從選擇查詢回傳以下陣列:
[{id=1, name=JENNY, country=LB}, {id=2, name=AMIGOSCODE, country=UK}]
id 是bigint資料庫中的型別,但是 name 是型別string
執行以下代碼時:
@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}");
}
}
我收到以下錯誤:
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')
我認為這是因為 hashmapHashMap<String, String>具有型別字串,但我不知道該怎么做,因為我在 hashMap 中有混合型別。
我還有一堂課:
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 "]";
}
}
當嘗試將 的型別更改為empId“long”時,它也不起作用。
我將不勝感激任何幫助!提前致謝!
uj5u.com熱心網友回復:
你有沒有嘗試轉換
ArrayList<HashMap<String, String>>
到
ArrayList<HashMap<String, Object>>
當您執行 data.get() 時,根據您的欄位名稱發布該資訊,您可以通過實體檢查將其轉換為所需的型別。
另外,如果要檢查型別:可以在每個欄位訪問之前添加日志
Object obj = data.get(<field>);
log(obj.getClass().getName());
希望這是有幫助的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/337133.html
標籤:爪哇 PostgreSQL 哈希图 阿帕奇骆驼
