簡體   English   中英

如何發送JSON對象到Rest Web服務?

[英]How to send JSON object to rest webservice?

我在http://localhost:8600/rest/student/details有一個REST Web服務,它將把學生的詳細信息更新到數據庫中。 我必須在方法發布中以JSON格式發送詳細信息。

以下是生成該json消息的方法

private void createOrUpdateStudent(WebResource service) {
    Gson gson = new Gson();

    StudentImpl student= new StudentImpl ();

    student.setId(6);
    student.setName("Godwin");
    student.setSex("Male");     

    StudentView view = new StudentView(student);        

    System.out.println("::::::"+service.path("/rest/student").path("/details").type("application/json").post(ClientResponse.class, gson.toJson(view)));
}

這里的studentView類如下。

@XmlRootElement(name="clusterZipcode")
public class StudentView {  

    public Integer id;
    public String name;
    public String sex;


   public StudentView() {}

   public StudentView(StudentImpl  student) {
        this.id = student.getId();
        this.name = student.getName();
        this.sex = student.getSex();

   }

像上面一樣發送時,我收到一條錯誤消息,指出Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'Name' cannot be null

我是否正確傳遞json值,或者是否有其他方法發送json消息,請建議我。

我看不到您的帖子,您正在哪里調用數據庫。 我所看到的是,您正在創建一個Student,然后從那里創建一個視圖(假設正在調用此方法)。 您得到的錯誤與以下事實有關:在某個時候,您正在調用數據庫以在(可能是)學生模型中輸入新行。 如果您已進入休眠狀態,是否可以確保在為學生撥打的電話之間以及在致電setName之前的任何時候都沒有呼叫.save或.update?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM