[英]UnrecognizedPropertyException: Unrecognized field not marked as ignorable at Source: org.apache.catalina.connector.CoyoteInputStream@14ec141
I am making rest web-services my code is:我正在制作 rest web-services 我的代码是:
@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:但是当我用这个 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; 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"])行:2,列:15](通过引用链:com.tazligen.model.GroupMemberMap["userId"])
My GrouMemberMap model class is:我的 GrouMemberMap model class 是:
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与 json
[{
"userId":"3",
"userName":"John"}]
but this works alright:/但这没问题:/
Need Someone help.需要有人帮助。
I can make following observations after looking at GroupMemberMap
Class:查看GroupMemberMap
类后,我可以进行以下观察:
UserId
is incorrect. UserId
Getter-Setter 不正确。 Also, you can add optional @JsonIgnoreProperties
to ignore all other unknown fields.此外,您可以添加可选的@JsonIgnoreProperties
以忽略所有其他未知字段。
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;
}
}
I was using lombok to generate getters and setters with @Getter and @Setter annotation.我正在使用 lombok 生成带有 @Getter 和 @Setter 注释的 getter 和 setter。 Now what solved a similar issue for me was converting data type of a field from primary java type boolean to Boolean.现在为我解决了类似问题的是将字段的数据类型从主要的 java 类型 boolean 转换为 Boolean。 Lombok only generated a getter for it only if I used Boolean fieldName. Lombok 仅在我使用 Boolean fieldName 时才为它生成一个 getter。
Just modify userId and groupId public
.只需修改 userId 和 groupId public
。 By default, Jackson works on public member variables.默认情况下,Jackson 处理公共成员变量。
I think JsonIgnoreProperties is not solution as it is used to ignore whatever it doesn't recognize.我认为 JsonIgnoreProperties 不是解决方案,因为它用于忽略它无法识别的任何内容。
public String userId;
public String groupId;
above solution worked for me and I had string array inside json object. I used JsonIgnore properties and replaced square brackets for the objects inside json object..上面的解决方案对我有用,我在 json object 中有字符串数组。我使用 JsonIgnore 属性并为 json object 中的对象替换了方括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.