【问题标题】:GWT RPC attribute emptyGWT RPC 属性为空
【发布时间】:2013-10-19 01:36:38
【问题描述】:

在我的 GWT 应用程序中,必须跟踪对我的休眠对象所做的更改,所以我有这个简单的 POJO 来将修改传输到服务器端,在那里它们将被记录:

public class ModifiedValueReference implements Serializable {
    private static final long serialVersionUID = 6144012539285913980L;

    private Serializable oldValue;
    private Serializable newValue;

    public ModifiedValueReference() {
        super();
    }

    public ModifiedValueReference(Serializable oldValue, Serializable newValue) {
        this();
        setOldValue(oldValue);
        setNewValue(newValue);
    }

    public Serializable getOldValue() {
        return oldValue;
    }

    public void setOldValue(Serializable oldValue) {
        this.oldValue = oldValue;
    }

    public Serializable getNewValue() {
        return newValue;
    }

    public void setNewValue(Serializable newValue) {
        this.newValue = newValue;
    }

}

属性oldValuenewValue 的类型为Serializable,因此可以存储我的整数、字符串、日期和布尔值,以及其他几个Hibernate 对象。

通过使用记录修改的特殊设置方法来实现跟踪(例如:使用setFirstNameLog() 而不是下面的setFirstName()):

public class Person {

    private String firstname;

    private Map<String, ModifiedValueReference> modifications = 
            new HashMap<String, ModifiedValueReference>(15);

    public void addModification(String key, Serializable oldValue, Serializable newValue) {
        if (key != null && !key.isEmpty()) {
            modifications.put(key, 
                    new ModifiedValueReference(oldValue, newValue));
        }
    }

    public void setFirstnameLog(String firstname) {
        addModification("First Name", getFirstname(), firstname);
        setFirstname(firstname);
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;     
    }
    public String getFirstname() {
        return this.firstname;
    }
}

ModifiedValueReference对象通过GWT RPC到达服务器端时,oldValuenewValue是空的! 为什么?

这些字段在客户端用字符串填充。在服务器端,它们不是 null,而是空字符串。

【问题讨论】:

  • 也许更多的代码会有所帮助。可能是实际的服务调用和服务器端实现。
  • @enrybo 感谢您的建议。由于该代码正在工作,因此我在家中制作了一个示例 GWT 项目以尝试重现该问题。我在家的代码运行良好,所以我怀疑工作中的其他一些代码导致了这个问题。我们有 hibernate 和 gilead 在工作,所以任何事情都可能导致问题。如果你愿意,我可以把我现在写的代码贴出来。
  • @enrybo,我想通了。我没有modifications 的设置器,这导致它无法被服务器端的 GWT RPC 重建。

标签: gwt serialization gwt-rpc


【解决方案1】:

问题是我的Person 类中的modifications 映射没有setter 方法(例如:setModifications(Map&lt;String, ModifiedValueReference&gt;) 方法)。

因此,当通过 RPC 方法保存 Person 对象时,GWT RPC 无法在服务器端重建地图。

【讨论】:

  • 这真的很奇怪,因为 GWT 序列化不需要 getter 或 setter。所有本身可序列化的非最终、非瞬态字段都将被序列化。
  • @Strelok,我也不这么认为,但是调用com.google.gwt.user.server.rpc.impl.ServerSerializationStreamReader.getSetters(Class&lt;?&gt;)com.google.gwt.user.server.rpc.impl.ServerSerializationStreamReader.deserializeClass(Class&lt;?&gt;, Object) 另有说法。我在调试com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(String) 之后发生的所有事情时发现了这一点。
  • 谢谢!因为缺少二传手而浪费了这么多时间!
  • @Carcamano 太棒了!很高兴知道 3 年前的答案仍在帮助人们!
猜你喜欢
  • 2011-06-11
  • 1970-01-01
  • 2012-02-24
  • 2016-02-04
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
相关资源
最近更新 更多