简体   繁体   中英

JSON output using Hibernate and Spring MVC not as expected

I have a RestService Spring MVC.I am using hibernate and running project on JBOSS.My JSON output should be in the format as

{
            "iteration": "2017 Sprint 1",
            "project": "MDM - Core & Integration",
            "isd": "23/12/2016",
            "ied": "16/01/2017",....

But I am getting only results as

"2017 Sprint 1",
"MDM - Core & Integration",...

My code is as follows: IterationInfo.java

package pojoclasses;

import java.util.Date;

public class IterationInfo
{
private int iteration_id;
private int project_id;
private String iteration_name;
private Date isd;
private Date ied;

// getter and setter section

}

PageInfo.java

package pojoclasses;

import java.util.Date;

public class PageInfo 
{
private int comment_id;
private String comment_text;
private String comment_type;
private int user_id;
private int retrospective_id;
private Date creation_date;
private Date modification_date;   

//getters/setters..

projectInfo.java

package pojoclasses;

public class ProjectInfo 
{
private int project_id;
private String project_name;

//getters/setters

Retrospectiveinfo.java

package pojoclasses;

import java.util.Date;

public class RetrospectiveInfo 
{
private int retrospective_id;
private Date retrospective_date;
private int project_id;
private int iteration_id;
private int user_id;

//getters/setters

UserInfo.java

package pojoclasses;

public class UserInfo 
{
private int user_id;
private String user_name;
private String email_id;
private int rally_objectid;

//getters/setters

MainControllerClass.java

package packagecontroller;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import binderclass.Details1;

@Controller
@RequestMapping("/json/retrospective")
public class MainControllerClass 
{
@RequestMapping(value="{userid}", method = RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody List<Details1> getInfoInJSON(@PathVariable int userid)

    {
    Configuration con = new Configuration();
    con.configure("hibernate.cfg.xml");
    SessionFactory SF = con.buildSessionFactory();
    Session session= SF.openSession();
    Query queryinfo=session.createQuery("select itr.iteration_name,prj.project_name,itr.isd,itr.ied from RetrospectiveInfo retro,IterationInfo itr,ProjectInfo prj where retro.retrospective_id ="+userid+" and retro.project_id = prj.project_id and retro.iteration_id = itr.iteration_id");
    List<Details1> pagedetails=queryinfo.list();
    //Query to be fired..
    session.close();
    SF.close();
    return pagedetails;
}

Details1.java

package binderclass;

import java.util.Date;

public class Details1 
{
private String iteration;
private String project;
private Date isd;
private Date ied;

//getters/setters

I have created the details1.java just to get info from objects from multiple POJOs in list.Please help

`

The below code will only give you result that you are seeing .

Query queryinfo=session.createQuery("select itr.iteration_name,prj.project_name,itr.isd,itr.ied from RetrospectiveInfo retro,IterationInfo itr,ProjectInfo prj where retro.retrospective_id ="+userid+" and retro.project_id = prj.project_id and retro.iteration_id = itr.iteration_id");
List<Details1> pagedetails=queryinfo.list();

You can improve that code in multiple levels

1> Use JPA entities

2> Use criteria API instead of firing queries

3>Just pass that JPA entity into the controller instead of mapping it to another POJO

4> Use different layer to access the database

Following link can be useful https://www.petrikainulainen.net/programming/spring-framework/creating-a-rest-api-with-spring-boot-and-mongodb/

@ResponseBody annotation doesn't annotate List . It annotates the method, just as RequestMapping does, annotate with method.

 @RequestMapping(value="{userid}", method =  RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)

@ResponseBody
   public List<Details1> getInfoInJSON(@PathVariable int userid)
   {
        // your code
   }

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