2012-01-24 5 views
1

私はAutoBeanを使用してJSONにデータをコード/デコードしています。これは以前のGWTバージョンでも問題ありませんでした。私の意見では、AutoBeanはJSONを扱うのにとても便利で便利なツールです。 GWT ver.2.4.0以降、この機能は変更されており、私はコードでそれを復元するのに時間を費やしました。しかし、1つの部分だけが固定されていないままです - 注釈@PropertyName。この注釈は、プロパティに「エイリアス」を追加するために使用されます。これは、多くのネットワークトラフィックを節約します。そして今、例外がスローされます。コード例は以下の通りです:GWT AutoBean:アノテーション@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 

コードのこの作品は、レガシーコードで完全に働きました。

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に)私は例外を取得します。

公式のドキュメントは更新されますが、まだ古いコード例が残っています。

誰かがこの問題の解決に手伝ってもらえますか?アドバイスありがとうございます。

私はGWT verを使用しています。 2.4.0、GAE ver。 1.6.1。

答えて

6

設定メソッドに@PropertyName( "XXXX")を入れる必要がありました。試してみる。

+1

ありがとうございました!解決策はとても簡単でした...あなたが悲しいときに、私は注釈をセッターとゲッターの両方に置いて動作させました!したがって、 の代わりに '//名前 @PropertyName(" n ") パブリックString getName(); 公共ボイドのsetName(文字列名); ' 私はこの ' //名前 @PropertyName( "N") 公共の文字列のgetName()のようにそれを置きます。 @PropertyName( "n") パブリックvoid setName(String name); ' –

関連する問題