我已经成功实现了Jersey的简单WebService调用(queryparams和简单对象),但是当我尝试sync
存储或save
记录时,jersey无法理解作者的身份rootProperty
。它不知道从哪里开始,也无法Consume
发送ExtJS发送的json记录。不幸的rootProperty
是,当您将数据转换为JSON时,根据ExtJS是强制性的,所以我不能没有它。我显然可以Consumes(MediaType.TEXT_PLAIN)
自己使用和变换对象,但是我试图利用Jersey的自动对象编组等优势。
用于.sync
存储数据或.save
记录的一般惯例是什么?
编辑:我不认为问题出在我的对象的JSON。我的商店的代理配置如下:
proxy: {
type: 'ajax',
url: 'ext/AnnouncementHelper/myFunction',
headers: {'Content-Type': 'application/json;charset=utf-8'},
reader: {
type: 'json',
rootProperty: 'data',
messageProperty: 'processMessage'
},
writer: {
type: 'json',
rootProperty: 'data',
encode: true,
writeAllFields: true
}
这样做是使用以下参数创建POST请求:
data={id: 1, description: 'status 1'}
我的模型配置如下:
public class AnnouncementStatus {
private int id;
private String description;
@JsonCreator
public AnnouncementStatus() {
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
我的函数声明是:
@POST
@Path("myFunction")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response myFunction(AnnouncementStatus status)
这给我在Java中的以下错误:
org.codehaus.jackson.JsonParseException:意外字符(“ d”(代码100)):预期为有效值(数字,字符串,数组,对象,“ true”,“ false”或“ null”)
我猜杰克逊不喜欢我的对象以开头,data=
但是我无法避免这一点,因为在使用存储和记录时必须具有root属性。
尽管找不到手头的问题,但我找到了解决方案。
我仍然不知道如何使用jersey读取作者的root属性,但是我发现我不再需要了,因为如果我们更改代理的作家,encoding: false
则不必设置a rootProperty
,而是将记录作为数组发送。Jersey的序列化程序可以理解这一点,并且可以正确地将数据解码为对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句