2017-01-23 48 views
0

我有一個RestService春天MVC.I正在使用的JBOSS.My JSON輸出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只是爲了讓來自於list.Please多個POJO對象信息幫助

`

+0

你有沒有在classpath中使用Jackson,如果你使用maven,你有沒有Jackson作爲依賴? –

+0

在json中缺少的字段是否在Details1類中有getters? – Massimo

+0

@ EssexBoy ..我已經添加傑克遜作爲依賴 – Akshay

回答

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>使用JPA實體

2>使用標準的API,而不是射擊查詢

3>只是傳遞JPA實體進入控制器而不是映射到另一個POJO

4>使用不同的層訪問數據庫

下面的鏈接可能是有用的 https://www.petrikainulainen.net/programming/spring-framework/creating-a-rest-api-with-spring-boot-and-mongodb/

0

@ResponseBody標註沒有標註列表。它註釋該方法,就像RequestMapping一樣,使用方法進行註釋。

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

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

我試着進行更改,因爲你說..我沒有工作 – Akshay

+0

首先檢查調試模式下pagedetails的值 – Onkar