我有一个基本项目,当教师登录系统时可以添加课程,当学生登录系统时可以选择课程并添加到课程列表中。我使用MongoDB作为数据库。我的问题是,当我想在学生课程列表中添加新课程时,我收到了一些错误。这是我更新课程列表的PUT注释:
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/")
public User updateCourses(User user) {
MongoCollection usersCollection = MongoConnector.getUserCollection();
UserService userService = new UserService(usersCollection);
User updatedUser = userService.updateCourses(user);
return updatedUser;
}
这是我的用户模型:
public class User {
private String email;
private String pass;
private String type;
private List<String> courses;
public User(){
}
public User(String email, String pass, String type, List<String> courses) {
this.email = email;
this.pass = pass;
this.type = type;
this.courses = courses;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<String> getCourses() {
return courses;
}
public void setCourses(List<String> courses) {
this.courses = courses;
}}
我的Json模特:
{
"email": "deneme@student.com",
"pass": "4",
"type": "student",
"courses": [
"Deneme1",
"Deneme2",
"Deneme3",
"Deneme4"
]
}
最后我的userService funstion将新课程添加到学生的课程列表中:
public User updateCourses (User user){
userCollection.updateOne(eq("email", user.getEmail()),Updates.addToSet("courses", user.getCourses()));
return user;
}
我从邮递员处得到此错误: enter image description here
正如我搜索过的那样,我发现我的代码返回String而不是Json,这可能是问题所在。但是我也尝试用Gson将用户转换为Json但是没有用。我怎么能传递这个问题?
提前致谢