我如何使用 http post 将两个参数传递给 restful 网络服务? UnrecognizedPropertyException:无法识别的字段,未标记为可忽略

How can i passing two parameters to restful web service using http post? UnrecognizedPropertyException: Unrecognized field , not marked as ignorable

我的课程 pojo 是 ;

public class Course {
private int cid;
private String name;
private String code;
private int credit;

//Getters Setters

}

服务:

@RequestMapping(value="/addcourse" , method=RequestMethod.POST)
public @ResponseBody Response<Course> addCoursetoUser(@RequestBody Course course, @RequestBody User user) throws SQLException{

    if(new CourseDAO().addCoursetoUser(course, user))
        return new Response<>(...);
    else
        return new Response<>(...);

}

我正在尝试将此 json 值发送到我的 Web 服务,但出现此错误: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: 无法识别的字段 "cid" (Class com.spring.model.Course),未标记为可忽略

{
"id" :3,
"name" : "Algorithms",
"code" : "COM367",
"credit" : 3,
"cid" : 28,
"username" : "selin",
"password" : "ssgg"

}

我已经尝试了很多json,但我总是遇到这个错误。 提前致谢..

你不能。您需要将两个对象包装成一个对象(可能 CourseUserCourseUserRequest)。

该错误还暗示您的 Course class 缺少 Java 模型中的 cid 字段。

首先你需要为所有[=27=编写getter和setter方法] 你在你的 pojo 中删除的成员:

例如:

public class Course {
private int cid;

public int getCid()
{
      return this.cid ;
}

public void setCid(int cid)
{
    this.cid=cid;
}

}

第二 您的 post menthod 中不能有两个请求正文参数,您需要定义一个包含 Course 和 User Pojo 的 parant pojo,就像这样

public class MyClass{

private Course course ;

private User user ;

// getter setter for User and Course 

}

当然你的json会改变,如果你像这样使用:

{
"course.id" :3,
"course.name" : "Algorithms",
"course.code" : "COM367",
"course.credit" : 3,
"course.cid" : 28,
"user.username" : "selin",
"user.password" : "ssgg"
}