简体   繁体   中英

how to return json objects for list<POJO> in RestController

I have restful webservice returning list of pojo objects which is not generating correct json format.

The following is the code which is not return json object

@RequestMapping("/getThisWeekPlan")
public List<ShiftPlannerView> getThisWeekPlan() {

    return getShiftPlanRepo.fetchThisWeekShiftPlan();
}

My POJO where setting the result from JPA with hibernate using namedquery

    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 + "]";
    }


}

DB call

@Repository

public class GetShiftPlanRepoImpl implements GetShiftPlanRepo{

@PersistenceContext
EntityManager em;

@Override
public List<ShiftPlannerView> fetchThisWeekShiftPlan() {

    List<ShiftPlannerView> result= em.createNamedQuery("fetchByShiftPlannerThisWeek")
            .getResultList();
    return result;
}

}

The following is the response:

 [
    [
        "2018-04-16",
        "Elias",
        "I"
    ],
    [
        "2018-04-16",
        "Sithik",
        "II"
    ],
    [
        "2018-04-17",
        "Vikram Boya",
        "I"
    ],
    [

The following code changes brought me expected response

@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();
    }

Thanks to JB!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM