Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Hibernate和Spring MVC的JSON输出不符合预期_Java_Json_Hibernate_Spring Mvc - Fatal编程技术网

Java 使用Hibernate和Spring MVC的JSON输出不符合预期

Java 使用Hibernate和Spring MVC的JSON输出不符合预期,java,json,hibernate,spring-mvc,Java,Json,Hibernate,Spring Mvc,我有一个RestService Spring MVC。我正在使用hibernate并在JBOSS上运行项目。我的JSON输出的格式应该如下所示 { "iteration": "2017 Sprint 1", "project": "MDM - Core & Integration", "isd": "23/12/2016", "ied": "16/01/2017",.... 但我得到的只是结

我有一个RestService Spring MVC。我正在使用hibernate并在JBOSS上运行项目。我的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
retrospectVeInfo.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只是为了从列表中多个POJO的对象中获取信息。请帮助


`下面的代码只会给出您看到的结果

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();
queryqueryinfo=session.createQuery(“从RetrospectInfo retro、IterationInfo itr、ProjectInfo prj中选择itr.iteration\u名称、prj.project\u名称、itr.isd、itr.ied,其中retro.Retroduction\u id=“+userid+”和retro.project\u id=prj.project\u id和retro.iteration\u id=itr.iteration\u id”);
List pagedetails=queryinfo.List();
您可以在多个级别改进该代码

1> 使用JPA实体

2> 使用CriteriaAPI而不是触发查询

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

4> 使用不同的层访问数据库

下面的链接可能很有用
@ResponseBody注释不注释列表。它注释方法,就像RequestMapping使用方法注释一样

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

@ResponseBody
   public List<Details1> getInfoInJSON(@PathVariable int userid)
   {
        // your code
   }
@RequestMapping(value=“{userid}”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
@应答器
公共列表getInfoInJSON(@PathVariable int userid)
{
//你的代码
}

类路径中是否有Jackson,如果使用maven,是否有Jackson作为依赖项?json中缺少的字段在Details1类中是否有getter?@EssexBoy..我将Jackson添加为dependency@Massimo是的,他们有getter/setters@EssexBoy..anything您建议??首先在调试模式下检查pagedetails的值
 @RequestMapping(value="{userid}", method =  RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)

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