0

I am passing json string from restclient to resteasy webservice for model object user.

I have set Content-Type=application/json

and my json string in body is as follows,

{
    "id": "100",
    "email": "email",
    "add": [
        {
            "lastName": "lastName",
            "firstName": "firstName"
        },
        {
            "firstName": "firstName",
            "lastName": "lastName"
        }
    ]
}

If i remove add array object as below, then i am getting the response as expected.

{
    "id": "100",
    "email": "email",
}

When i try to send add array , server is throwing an exception saying,

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.model.add out of START_ARRAY token

I tried this way also, but its not working,

{
    "id": "100",
    "email": "email",
     "lastName": "Something Four",
     "firstName": "Something Five"
}

Then it is giving below error,

Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "lastName" (Class com.model.user), not marked as ignorable

My model objects are below,

public class user implements Serializable {
    @Id
    @Column(name="Id", unique=true, nullable=false)
    private int id;

    @Column(name="email", nullable=true, length=60)
    private String email;

    @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name="Id", nullable=false)
    private add add;
}



public class add implements Serializable {

    @Id
    @Column(name="Id", unique=true, nullable=false)
    private String id;

    @Column(name="LastName", nullable=true, length=128)
    private String lastName;

    @Column(name="FirstName", nullable=true, length=128)
    private String firstName;

}

My service class

@POST
    @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
     public Response addAccount(@FormParam("id") String id,
             @FormParam("lastName") String lastName,
             @FormParam("firstName") String firstName,
             @FormParam("email") String email{



            user.setId(id); 
            add.setLastName(lastName);
            add.setFirstName(firstName);
            user.setEmailAddress(email);
}

Can anyone help me in passing one model object inside other model object as an array in json string?

user1660325
  • 747
  • 4
  • 20
  • 35
  • Please show us the JSON you are sending. Jackson is telling you here this is not a JSON array. – fge Jun 18 '13 at 12:17
  • this is the json i am using ,{ "id": "100", "email": "email", "add": [ { "lastName": "lastName", "firstName": "firstName" }, { "firstName": "firstName", "lastName": "lastName" } ] } i have validated it using jsonlint and its showing as valid json.\ – user1660325 Jun 18 '13 at 12:24
  • That is not the question. I ask what you send as the _add_ array you have removed. – fge Jun 18 '13 at 12:26
  • How do you explain your first message then? "Can not deserialize instance of com.model.add out of START_ARRAY token" <-- this means Jackson expects a JSON object but you send a JSON array – fge Jun 18 '13 at 12:41
  • Read your second message again, it also says: "Unrecognized field "lastName" (Class com.model.user), not marked as ignorable" <-- this gives you a hint: it MAY be ignored, just use the right tool to have it ignored – fge Jun 18 '13 at 12:45
  • As a workaround, do not remove the array entirely, just empty it – fge Jun 18 '13 at 12:49

1 Answers1

3

I got the same error, the solution i found; the getters and setters were missing in my model class. Probably this will solve this issue.

techlearner
  • 109
  • 1
  • 4