使用Hibernate和Spring MVC的JSON输出不如预期

时间:2017-01-23 07:34:22

标签: java json hibernate spring-mvc

我有一个RestService Spring MVC。我在JBOSS上使用hibernate并运行项目。我的JSON输出应该是格式为

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

但我只得到结果

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

我的代码如下: 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

我创建了details1.java只是为了从列表中的多个POJO中的对象获取信息。请帮助

`

2 个答案:

答案 0 :(得分:0)

以下代码只会为您提供您所看到的结果。

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

您可以在多个级别改进该代码

1&GT;使用JPA实体

2 - ;使用条件API而不是触发查询

3&gt;只需将JPA实体传递到控制器,而不是将其映射到另一个POJO

4&GT;使用不同的层来访问数据库

以下链接可能很有用 https://www.petrikainulainen.net/programming/spring-framework/creating-a-rest-api-with-spring-boot-and-mongodb/

答案 1 :(得分:0)

@ResponseBody 注释未注释列表。它注释方法,就像RequestMapping一样,用方法注释。

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

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