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

4

10

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.

Dudeen answered 24/2, 2016 at 17:21 Comment(0)
P
21

I can make following observations after looking at GroupMemberMap Class:

  1. Constructor is missing.
  2. Getter-Setter for the 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;
    }    
}
Purser answered 24/2, 2016 at 17:30 Comment(5)
i have edited my question a bit in which u can see that i tried with a different model class but similar json pattern but that one worksDudeen
Updated the solution. Please try now.Purser
Your no 2 observation did the trick getter setter was wrong and the only reason why i ever missed it was because i had eclipse autogenerate it for me So i never thought it would be wrong so i never paid attension to that part .-_- Damn you Eclipse . Thanks a ton sir :)Dudeen
@Muhammad Your Welcome! :)Purser
Solved my problem as well.Perspex
B
0

Just modify userId and groupId public. By default, Jackson works on public member variables.

I think JsonIgnoreProperties is not solution as it is used to ignore whatever it doesn't recognize.

public String userId;
public String groupId;
Bocage answered 4/8, 2020 at 17:51 Comment(0)
F
0

I was using lombok to generate getters and setters with @Getter and @Setter annotation. Now what solved a similar issue for me was converting data type of a field from primary java type boolean to Boolean. Lombok only generated a getter for it only if I used Boolean fieldName.

Ferryman answered 27/8, 2021 at 10:22 Comment(0)
N
-1

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..

Natator answered 4/4, 2022 at 17:9 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Nonanonage
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewContraception

© 2022 - 2024 — McMap. All rights reserved.