简体   繁体   English

如何发送JSON对象到Rest Web服务?

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

I have a rest webservice at http://localhost:8600/rest/student/details which will update the details of student into database. 我在http://localhost:8600/rest/student/details有一个REST Web服务,它将把学生的详细信息更新到数据库中。 I have to send the details as JSON format in method post. 我必须在方法发布中以JSON格式发送详细信息。

The below is the method to generate that json message 以下是生成该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)));
}

where studentView class is as below. 这里的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();

   }

while sending like above i am getting an error stating Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'Name' cannot be null 像上面一样发送时,我收到一条错误消息,指出Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'Name' cannot be null

Is i am passing the json values correctly or if there is an alternate method to send the json message please suggest me. 我是否正确传递json值,或者是否有其他方法发送json消息,请建议我。

I cannot see on your post where are you doing the call to the database. 我看不到您的帖子,您正在哪里调用数据库。 All I can see is that you are creating a Student and then, from there, creating a view (assuming this method is being called). 我所看到的是,您正在创建一个Student,然后从那里创建一个视图(假设正在调用此方法)。 The error you are getting is related to the fact that, at some point, you are calling the database to enter a new row in (probably) your student Model. 您得到的错误与以下事实有关:在某个时候,您正在调用数据库以在(可能是)学生模型中输入新行。 If you have got hibernate, can you make sure that you are not calling .save or .update at any point between the call you get for the student and before you call setName? 如果您已进入休眠状态,是否可以确保在为学生拨打的电话之间以及在致电setName之前的任何时候都没有呼叫.save或.update?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM