【发布时间】:2017-01-23 07:34:21
【问题描述】:
我有一个 RestService Spring MVC。我在 JBOSS 上使用休眠和运行项目。我的 JSON 输出格式应为
{
"iteration": "2017 Sprint 1",
"project": "MDM - Core & Integration",
"isd": "23/12/2016",
"ied": "16/01/2017",....
但我得到的只是结果
"2017 Sprint 1",
"MDM - Core & Integration",...
我的代码如下: 迭代信息.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
用户信息.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 的对象中获取信息。请帮助
`
【问题讨论】:
-
类路径中有Jackson吗,如果你使用maven,你有Jackson作为依赖吗?
-
json中缺失的字段在Details1类中有getter吗?
-
@EssexBoy..我已将 Jackson 添加为依赖项
-
@Massimo 是的,他们有 getter/setter
-
@EssexBoy..你有什么建议吗??
标签: java json hibernate spring-mvc