我将 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/