引起:org.codehaus.jackson.map.JsonMappingException:无法从START_ARRAY令牌中反序列化com.model.user的实例

时间:2013-06-18 12:14:23

标签: java json model rest-client

我将json字符串从restclient传递给resteasy webservice,用于模型对象用户。

我已设置Content-Type = application / json

和我体内的json字符串如下,

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

如果我删除如下所示的添加数组对象,那么我将按预期获得响应。

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

当我尝试发送添加数组时,服务器正在抛出一个例外,

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

我也尝试过这种方式,但它不起作用,

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

然后它给出了以下错误,

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

我的模型对象在下面,

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;

}

我的服务类

@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);
}

任何人都可以帮助我将其他模型对象中的一个模型对象作为json字符串中的数组传递吗?

1 个答案:

答案 0 :(得分:2)

我得到了同样的错误,我发现的解决方案;我的模型类中缺少getter和setter。 这可能会解决这个问题。