java - 由 : org. codehaus.jackson.map.JsonMappingException : Can not deserialize instance of com. model.user 超出 START_ARRAY token 引起

标签 java json model rest-client

我将 json 字符串从 Restclient 传递到模型对象用户的 Resteasy WebService。

我已经设置了 Content-Type=application/json

我的body中的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 字符串中的数组传递到另一个模型对象中吗?

最佳答案

我遇到了同样的错误,我找到了解决方案;我的模型类中缺少 getter 和 setter。 也许这会解决这个问题。

关于java - 由 : org. codehaus.jackson.map.JsonMappingException : Can not deserialize instance of com. model.user 超出 START_ARRAY token 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17168590/

相关文章:

Javascript用html unicode替换tab字符进行JSON解析

r - R 中的模型语法 : how to input dynamic variables?

javascript - 如何使用回调函数修复错误?

java - 如何将 SQL 结果集输出限制为 1 个结果而不是每个查询结果?

java - 提高怪物数据库速度的方法

java - 在Pentaho Kettle中,如何检查文件名是否存在?

java - 您可以使用 GSON 的 JsonElement 对大多数但不是所有类成员执行 equals 比较吗

ruby-on-rails - 记录的哈希表示中的嵌套属性

forms - 我如何在 ionic 2 中将数据从带有表单的模态传递到主页?

带星号的 Java ProcessBuilder 命令