我有一个restful webservice返回pojo对象的列表,这些对象没有生成正确的json格式。
以下是不返回json对象的代码
@RequestMapping("/getThisWeekPlan")
public List<ShiftPlannerView> getThisWeekPlan() {
return getShiftPlanRepo.fetchThisWeekShiftPlan();
}
我的POJO使用namedquery
在hibernate中设置JPA的结果 public class ShiftPlannerView {
public ShiftPlannerView() {
}
private Date shiftPlannerDate;
private String resourceName;
private String shiftName;
public ShiftPlannerView(Date shiftPlannerDate, String resourceName, String shiftName) {
super();
this.shiftPlannerDate = shiftPlannerDate;
this.resourceName = resourceName;
this.shiftName = shiftName;
}
public Date getShiftPlannerDate() {
return shiftPlannerDate;
}
public void setShiftPlannerDate(Date shiftPlannerDate) {
this.shiftPlannerDate = shiftPlannerDate;
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
public String getShiftName() {
return shiftName;
}
public void setShiftName(String shiftName) {
this.shiftName = shiftName;
}
@Override
public String toString() {
return "ShiftPlannerView [shiftPlannerDate=" + shiftPlannerDate + ", resourceName=" + resourceName
+ ", shiftName=" + shiftName + "]";
}
}
数据库调用
@Repository
public class GetShiftPlanRepoImpl实现GetShiftPlanRepo {
@PersistenceContext
EntityManager em;
@Override
public List<ShiftPlannerView> fetchThisWeekShiftPlan() {
List<ShiftPlannerView> result= em.createNamedQuery("fetchByShiftPlannerThisWeek")
.getResultList();
return result;
}
}
以下是回复:
[
[
"2018-04-16",
"Elias",
"I"
],
[
"2018-04-16",
"Sithik",
"II"
],
[
"2018-04-17",
"Vikram Boya",
"I"
],
[
答案 0 :(得分:0)
以下代码更改为我带来了预期的响应
@Override
public List<ShiftPlannerView> fetchThisWeekShiftPlan() {
List<ShiftPlannerView> listOfShiftPlannerView=new ArrayList<>();
try {
Query q= em.createNativeQuery(ShiftPlannerConstants.THISWEEKSHIFTPLANQUERY);
List<Object[]> result=q.getResultList();
for(Object[] row : result){
ShiftPlannerView emp = new ShiftPlannerView();
Date workingDays = sdf.parse(row[0].toString());
emp.setShiftPlannerDate(workingDays);
emp.setResourceName(row[1].toString());
emp.setShiftName(row[2].toString());
listOfShiftPlannerView.add(emp);
}
}catch (Exception e) {
e.printStackTrace();
}
感谢JB!