java UnrecognizedPropertyException:无法识别的字段未在来源处标记为可忽略:org.apache.catalina.connector.CoyoteInputStream@14ec141

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35608861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:21:27  来源:igfitidea点击:

UnrecognizedPropertyException: Unrecognized field not marked as ignorable at Source: org.apache.catalina.connector.CoyoteInputStream@14ec141

javajson

提问by Syed Muhammad Oan

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 :

但是当我用这个 Json 调用它时:

[
{
    "userId":"3",
    "groupId":"4"
}
]

I'm getting following Exception:

我收到以下异常:

javax.servlet.ServletException: org.codehaus.Hymanson.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"])

javax.servlet.ServletException:org.codehaus.Hymanson.map.exc.UnrecognizedPropertyException:无法识别的字段“userId”(类 com.tazligen.model.GroupMemberMap),未在 [来源:org.apache.catalina.connector 标记为可忽略。 CoyoteInputStream@14ec141; 行:2,列:15](通过参考链:com.tazligen.model.GroupMemberMap["userId"])

My GrouMemberMap model class is :

我的 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;
}       }

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.

需要有人帮助。

回答by user2004685

I can make following observations after looking at GroupMemberMapClass:

查看GroupMemberMapClass后,我可以进行以下观察:

  1. Constructor is missing.
  2. Getter-Setter for the UserIdis incorrect.
  1. 缺少构造函数。
  2. 的 Getter-SetterUserId不正确。

Also, you can add optional @JsonIgnorePropertiesto ignore all other unknown fields.

此外,您可以添加 optional@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;
    }    
}