2016-03-31 75 views
0

我必須使用hibernate在數據庫中持久存儲一個POJO類社區。我在班上ManageCommunity這個方法:Gson轉換爲Java Pojo make values null

public void persistCommunity(String name,String domain,String idCustomer){ 
     Session session = sessionFactory.getCurrentSession(); 
     session.beginTransaction(); 
     Community aCommunity=new Community(); 
     aCommunity.setName(name); 
     aCommunity.setDomain(domain); 
     aCommunity.setIdCustomer(idCustomer); 
     // aCommunity.setIdCommunity(aCommunity.getIdCommunity()); 
     session.save(aCommunity); 
     session.getTransaction().commit(); 
    } 

在有doPost方法我把這種方法的servlet和我通過JSON的參數,由郵遞員送:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 

    Gson g = new Gson(); 
    System.out.println(CommunityHome.getBody(req)); 
    Community community = g.fromJson(CommunityHome.getBody(req), Community.class); 
    System.out.println("Community: "+community); 

    HttpSession session = req.getSession(true); 
    try { 
     ManageCommunity manageCommunity = new ManageCommunity(); 
     manageCommunity.persistCommunity(community.getName(),community.getDomain(),community.getIdCustomer()); 

    } catch (Exception e) { 

     e.printStackTrace(); 
    } 
} 

現在社會的值爲空,但如果我看到getBody(REQ)有json的

這是getBody:

private static String getBody(HttpServletRequest request) throws IOException { 

     String body = null; 
     StringBuilder stringBuilder = new StringBuilder(); 
     BufferedReader bufferedReader = null; 

     try { 
      InputStream inputStream = request.getInputStream(); 
      if (inputStream != null) { 
       bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
       char[] charBuffer = new char[128]; 
       int bytesRead = -1; 
       while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { 
        stringBuilder.append(charBuffer, 0, bytesRead); 
       } 
      } else { 
       stringBuilder.append(""); 
      } 
     } catch (IOException ex) { 
      throw ex; 
     } /*finally { 
      if (bufferedReader != null) { 
       try { 
        bufferedReader.close(); 
       } catch (IOException ex) { 
        throw ex; 
       } 
      } 
     }*/ 

     body = stringBuilder.toString(); 
     return body; 
    } 

這是POJO類:

public class Community{ 
    public Community(){ 
     UUID uuid=UUID.randomUUID(); 
     String random=uuid.toString(); 
     this.idCommunity=random; 
    } 



    public String getIdCommunity() { 
     return idCommunity; 
    } 
    public void setIdCommunity(String idCommunity) { 
     this.idCommunity = idCommunity; 
    } 
    public String getIdCustomer() { 
     return idCustomer; 
    } 
    public void setIdCustomer(String idCustomer) { 
     this.idCustomer = idCustomer; 
    } 
    public String getDomain() { 
     return domain; 
    } 
    public void setDomain(String domain) { 
     this.domain = domain; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 

    private String idCommunity; 
    private String idCustomer; 
    private String domain; 
    private String name; 

} 

CommunityHome.getBody(req)內容是:

{"idCustomer":"bb05ee35-0494-49e3-9e40-38d5dae10668", "name":"VenditaAbiti2", "domain":"Commerce2"}

+0

你的json不正確。現在,根級別不像您希望的那樣是社區。 – dambros

+0

我在網上驗證它是正確的。哪裏出錯? –

+0

你的問題不清楚,請讓你的帖子更清晰 – SpringLearner

回答

2

您的JSON改成這樣:

{"IdCustomer":"bb05ee35-0494-49e3-9e40-38d5dae10668", 
    "Name":"VenditaAbiti2", 
    "Domain":"Commerce2" 
} 

明白了在這個小例子的工作:

String json = "{\"IdCustomer\":\"bb05ee35-0494-49e3-9e40-38d5dae10668\",\"Name\":\"VenditaAbiti2\",\"Domain\":\"Commerce2\"}"; 
     Gson g = new Gson(); 
     Community community = g.fromJson(json, Community.class); 
     System.out.println("Community:" + community.toString()); 

    } 

    class Community { 
    @SerializedName("IdCustomer") 
    private String idCustomer; 

    @SerializedName("Name") 
    private String name; 

    @SerializedName("Domain") 
    private String domain; 

    @Override 
    public String toString() { 
     return "Community [idCustomer=" + idCustomer + ", name=" + name + ", domain=" + domain + "]"; 
    } 

//getter setter 

輸出:Community:Community [IdCustomer=bb05ee35-0494-49e3-9e40-38d5dae10668, Name=VenditaAbiti2, Domain=Commerce2]


EDIT1:

你的POJO的變量名應與您的JSON字符串的鍵值名。意思json鍵"IdCustomer"以大寫字母開頭。您的變量以小寫字母開頭。這不能匹配。 但你能夠註釋你的變量就像我的答案。在那裏你可以給出明確的名稱json密鑰

+0

不適合我...也許我有其他錯誤 –

+0

@FilomenaDelSorbo你會得到什麼錯誤? – Patrick

+0

可能與json啓動大寫字母和小寫字段有關。 – dambros