0

我有一个休息网络服务http://localhost:8600/rest/student/details,它将学生的详细信息更新到数据库中。我必须在方法 post 中以 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 消息,请建议我。

4

1 回答 1

0

我在您的帖子上看不到您在哪里调用数据库。我所能看到的是您正在创建一个学生,然后从那里创建一个视图(假设正在调用此方法)。您遇到的错误与以下事实有关:在某些时候,您正在调用数据库以在(可能)您的学生模型中输入新行。如果你有休眠,你能确保在你为学生接到的电话和你调用 setName 之前的任何时候都没有调用 .save 或 .update 吗?

于 2013-08-01T06:30:41.973 回答