我正在制作休息网络服务,我的代码是:
@Path("/add")
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response addMembers(List<GroupMemberMap> groupMemberMaps){
String message = "";
System.out.println("Inside addMembers of class "+this.toString());
try {
DBConnection.insertMembers(groupMemberMaps);
message = "Member(s) added";
return Response.status(Status.CREATED)
.entity(message)
.type(MediaType.TEXT_PLAIN)
.build();
} catch(SQLException sqle){
System.out.println("addMembers catch sqle");
message = "A problem occured while adding members : "+sqle.getMessage();
return Response.status(Status.INTERNAL_SERVER_ERROR)
.entity(message)
.type(MediaType.TEXT_PLAIN)
.build();
}
catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Inside addMembers catch "+e.getMessage());
message = "A problem occured while adding members : "+e.getMessage();
return Response.status(Status.INTERNAL_SERVER_ERROR)
.entity(message)
.type(MediaType.TEXT_PLAIN)
.build();
}
}
但是当我用这个 Json 调用它时:
[
{
"userId":"3",
"groupId":"4"
}
]
我收到以下异常:
javax.servlet.ServletException:org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“userId”(类 com.tazligen.model.GroupMemberMap),在 [来源:org.apache.catalina.connector。 CoyoteInputStream@14ec141; 行:2,列:15](通过引用链:com.tazligen.model.GroupMemberMap["userId"])
我的 GrouMemberMap 模型类是:
package com.tazligen.model;
@XmlRootElement
public class GroupMemberMap {
private String userId;
private String groupId;
public String getUserid() {
return userId;
}
public void setUserid(String userId) {
this.userId = userId;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
} }
我尝试了另一种方法,就像这样:
@Path("/membertest")
@POST
public String test(List<User> members){
return "Test subresource members working";
}
带json
[{
"userId":"3",
"userName":"John"}]
但这没问题:/
需要有人帮助。