I am making rest web-services my code is:
@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();
}
}
but when i call it with this Json :
[
{
"userId":"3",
"groupId":"4"
}
]
I'm getting following Exception:
javax.servlet.ServletException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "userId" (Class com.tazligen.model.GroupMemberMap), not marked as ignorable at [Source: org.apache.catalina.connector.CoyoteInputStream@14ec141; line: 2, column: 15] (through reference chain: com.tazligen.model.GroupMemberMap["userId"])
My GrouMemberMap model class is :
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;
} }
I have tried another method just like this :
@Path("/membertest")
@POST
public String test(List<User> members){
return "Test subresource members working";
}
with json
[{
"userId":"3",
"userName":"John"}]
but this works alright :/
Need Someone help.
I can make following observations after looking at GroupMemberMap
Class:
UserId
is incorrect.Also, you can add optional @JsonIgnoreProperties
to ignore all other unknown fields.
Here is the corrected code snippet:
package com.tazligen.model;
@XmlRootElement
@JsonIgnoreProperties(ignoreUnknown = true)
public class GroupMemberMap {
@JsonProperty("userId")
private String userId;
@JsonProperty("groupId")
private String groupId;
/* Add Constructor */
public GroupMemberMap() {}
/* Corrected Name */
public String getUserId() {
return userId;
}
/* Corrected Name */
public void setUserId(String userId) {
this.userId = userId;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
}