首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GWT AutoBean:注释@PropertyName不再工作了

GWT AutoBean:注释@PropertyName不再工作了
EN

Stack Overflow用户
提问于 2012-01-24 09:29:28
回答 1查看 1.6K关注 0票数 1

我使用AutoBean将数据编码/解码为JSON,这在以前的GWT版本中是可以接受的。在我看来,AutoBean是处理JSON的非常好和方便的工具。自从GWT ver.2.4.0以来,这个功能已经改变了,我花了一些时间在代码中恢复它。但是只有一个部分保持不变-注释@PropertyName。此注释用于向属性添加“别名”。它节省了很多网络流量。现在它抛出了一个异常。代码示例如下:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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中)我得到了一个例外:

代码语言:javascript
复制
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 ver。2.4.0,GAE ver.1.6.1.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-24 20:18:43

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

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8984490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档