【问题标题】:Reading ExtJS writer root property using Jersey rest web service使用 Jersey REST Web 服务读取 ExtJS 编写器根属性
【发布时间】:2015-04-06 15:00:06
【问题描述】:

我已经成功地为简单的 WebService 调用(查询参数和简单对象)实现了 Jersey,但是当我尝试 sync 存储或 save 记录时,泽西不理解作者的 rootProperty。它不知道从哪里开始,也无法Consume ExtJS 正在发送的 json 记录。不幸的是,当您将数据转换为 JSON 时,根据 ExtJS,rootProperty 是强制性的,所以我不能没有它。我显然可以使用 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: Unexpected character ('d' (code 100)): 需要一个有效值(数字、字符串、数组、对象、'true'、'false' 或 'null')

我猜杰克逊不喜欢我的对象以data= 开头,但我无法避免这种情况,因为在使用存储和记录时必须具有根属性。

【问题讨论】:

    标签: java web-services rest extjs jersey


    【解决方案1】:

    我找到了解决问题的方法,但还没有解决手头的问题。

    我仍然不知道如何使用 jersey 读取 writer 的 root 属性,但我发现我不再需要,因为如果我们将代理的 writer 更改为 encoding: false,则不必设置 rootProperty它只是将记录作为数组发送。 Jersey 的序列化器可以理解这一点,它可以正确地将数据解码为对象。

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 2012-09-10
      • 1970-01-01
      • 2011-01-01
      • 2012-04-13
      • 2023-04-06
      • 2014-12-05
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多