我有一个JSON。我正在使用hibernate,在JBOSS.My上运行项目的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/settersRetrospectiveinfo.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/settersUserInfo.java
package pojoclasses;
public class UserInfo
{
private int user_id;
private String user_name;
private String email_id;
private int rally_objectid;
//getters/settersMainControllerClass.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只是为了从list.Please帮助中来自多个POJO的对象获取信息
`
发布于 2017-01-23 15:57:55
下面的代码只会给出你所看到的结果。
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>使用JPA实体
2>使用criteria API而不是触发查询
3>Just将JPA实体传递到控制器中,而不是将其映射到另一个POJO
4>使用不同的层来访问数据库
发布于 2017-01-23 16:03:44
@ResponseBody注释不注释列表。它注释方法,就像RequestMapping做的那样,用方法注释。
@RequestMapping(value="{userid}", method = RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Details1> getInfoInJSON(@PathVariable int userid)
{
// your code
}https://stackoverflow.com/questions/41801236
复制相似问题