【问题标题】:GWT AutoBean: annotation @PropertyName doesn't work anymoreGWT AutoBean:注释@PropertyName 不再起作用
【发布时间】:2012-01-24 09:29:28
【问题描述】:

我使用AutoBean 将数据编码/解码为 JSON,这在以前的 GWT 版本中没有问题。在我看来,AutoBean 是处理 JSON 的非常好的和方便的工具。自 GWT ver.2.4.0 起,这个功能发生了变化,我花了一些时间在我的代码中恢复它。但只有一部分仍未修复——注解@PropertyName。此注释用于向属性添加“别名”。它节省了大量的网络流量。现在它抛出一个异常。代码示例如下:

import com.google.web.bindery.autobean.shared.AutoBean.PropertyName;

public interface IPersonInfo {

    // Name
    @PropertyName("n")
    public String getName();
    public void setName(String name);

    // Surname
    @PropertyName("s")
    public String getSurname();
    public void setSurname(String surname);

    // other properties...
}

然后我尝试以这种方式将其解码为 JSON:

AutoBean<IPersonInfo> user = factory.user();

// clone the userDto (it's a new way to clone an object in ver 2.4.0
// instad of deprecated clone() method)
Splittable data = AutoBeanCodex.encode(user);
IPersonInfo userDto = AutoBeanCodex.decode(factory, IPersonInfo.class, data).as();

userDto.setName("Name");
userDto.setSurname("Surname");
//... other properties

这段代码在遗留代码中完美运行。但是现在(在 GWT 2.4.0 中)我得到了一个例外:

java.lang.IllegalArgumentException: name
    at com.google.web.bindery.autobean.shared.impl.AutoBeanCodexImpl.doCoderFor(AutoBeanCodexImpl.java:524)
    at com.google.web.bindery.autobean.shared.impl.AbstractAutoBean.setProperty(AbstractAutoBean.java:276)
    at com.google.web.bindery.autobean.vm.impl.ProxyAutoBean.setProperty(ProxyAutoBean.java:253)
    at com.google.web.bindery.autobean.vm.impl.BeanMethod$3.invoke(BeanMethod.java:103)
    at com.google.web.bindery.autobean.vm.impl.SimpleBeanHandler.invoke(SimpleBeanHandler.java:43)
    at $Proxy74.setName(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:104)
    at com.google.web.bindery.autobean.vm.impl.ShimHandler.invoke(ShimHandler.java:81)
    at $Proxy74.setName(Unknown Source)

如果我从界面中删除 @PropertyName,则不会发生异常。

我还在等待,官方文档会更新,但它仍然保留旧代码示例。

有人可以帮我解决这个问题吗?谢谢你的建议。

我使用 GWT 版本。 2.4.0,GAE 版本。 1.6.1.

【问题讨论】:

    标签: java gwt annotations autobean


    【解决方案1】:

    我还需要将@PropertyName("XXXX") 放在我的设置方法上。试试看吧。

    【讨论】:

    • 谢谢!解决方案是如此简单......正如你所难过的那样,我将注释放在 setter 和 getter 上,它可以工作!所以不是// Name @PropertyName("n") public String getName(); public void setName(String name);,而是这样// Name @PropertyName("n") public String getName(); @PropertyName("n") public void setName(String name);
    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 2013-09-20
    • 2020-10-09
    • 2012-09-19
    • 2015-09-14
    相关资源
    最近更新 更多