繁体   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