2012-09-07 81 views
1

我需要通過GWT RPC連接作爲通用傳遞一個類的對象,但似乎RPC不與它合作。該類使用java.io.Serializable進行序列化。我使用gwt IsSerializable檢查過它,但我仍然有錯誤。GWT RPC序列化泛型類

這裏是我的代碼

MySource.java

@SuppressWarnings("serial") 
@PersistenceCapable 
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE) 
public abstract class MySource implements Serializable { 
@PrimaryKey 
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
private Long id; 
@Persistent 
private String userId; 
@Persistent 
private String title; 
@Persistent 
private String description; 
@Persistent 
private String blobKey; 
@Persistent 
private String youtubeLink; 
@Persistent 
private String personalLink; 
@Persistent 
private Date submitedDate; 
@Persistent 
private float price; 


public float getPrice() { 
    return price; 
} 

public void setPrice(float price) { 
    this.price = price; 
} 

public String getUserId() { 
    return userId; 
} 

public void setUserId(String userId) { 
    this.userId = userId; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getBlobKey() { 
    return blobKey; 
} 

public void setBlobKey(String blobKey) { 
    this.blobKey = blobKey; 
} 

public String getYoutubeLink() { 
    return youtubeLink; 
} 

public void setYoutubeLink(String youtubeLink) { 
    this.youtubeLink = youtubeLink; 
} 

public String getPersonalLink() { 
    return personalLink; 
} 

public void setPersonalLink(String personalLink) { 
    this.personalLink = personalLink; 
} 

public Date getSubmitedDate() { 
    return submitedDate; 
} 

public void setSubmitedDate(Date submitedDate) { 
    this.submitedDate = submitedDate; 
} 

public Long getId() { 
    return id; 
} 

} 

AndroidSource.java

@SuppressWarnings("serial") 
@PersistenceCapable 
public class AndroidSource extends MySource{ 

public AndroidSource() { 
    super(); 
} 

} 

CategoryBrowseService.java這是remoteservice模型

@RemoteServiceRelativePath("categoryService") 
public interface CategoryBrowseService extends RemoteService{ 


ArrayList<MySource> getSourceList(Class<? extends MySource> classType); 
} 

CategoryBrowseServiceAsync.java 公共接口CategoryBrowseServiceAsync {

void getSourceList(Class<? extends MySource> classType, 
     AsyncCallback<ArrayList<MySource>> callback); 

} 

CategoryBrowsePresenter.java其中RPC調用

private void retrieveSources(Class<? extends MySource> classType) { 
    CategoryBrowseServiceAsync rpcService = GWT.create(CategoryBrowseService.class); 

    rpcService.getSourceList(classType, new AsyncCallback<ArrayList<MySource>>() { 

     @Override 
     public void onFailure(Throwable caught) { 
      Window.alert("Ooops!!!Sorry!Something went wrong.I am still beta!"); 
     } 

     @Override 
     public void onSuccess(ArrayList<MySource> result) { 
      sourceList = result; 
      display.setSourceContent(sourceList); 
     } 
    }); 
} 

CategoryBrowseServiceImpl.java

@SuppressWarnings("serial") 
public class CategoryBrowseServiceImpl extends RemoteServiceServlet implements CategoryBrowseService{ 

    private SourceDatastore dataStore; 


    public CategoryBrowseServiceImpl() { 
    dataStore = new SourceDatastore(); 
    } 

    @Override 
    public ArrayList<MySource> getSourceList(Class<? extends MySource> classType) { 

     return dataStore.getSources(classType); 

    } 

} 

這裏是我的錯誤。

Compiling module com.sourcebay.SourceBay 
    Scanning for additional dependencies: file:/home/santaris/workspace/SourceBay/src/com/sourcebay/client/presenter/mybay/browse/CategoryBrowsePresenter.java 
     Computing all possible rebind results for 'com.sourcebay.client.model.mybay.browse.CategoryBrowseService' 
     Rebinding com.sourcebay.client.model.mybay.browse.CategoryBrowseService 
      Invoking generator com.google.gwt.user.rebind.rpc.ServiceInterfaceProxyGenerator 
       Generating client proxy for remote service interface 'com.sourcebay.client.model.mybay.browse.CategoryBrowseService' 
        [ERROR] java.lang.Class<T> is not assignable to 'com.google.gwt.user.client.rpc.IsSerializable' or 'java.io.Serializable' nor does it have a custom field serializer (reached via java.lang.Class<? extends com.sourcebay.shared.source.MySource>) 
        [ERROR] java.lang.Class<T> has no available instantiable subtypes. (reached via java.lang.Class<? extends com.sourcebay.shared.source.MySource>) 
        [ERROR] subtype java.lang.Class<T> is not assignable to 'com.google.gwt.user.client.rpc.IsSerializable' or 'java.io.Serializable' nor does it have a custom field serializer (reached via java.lang.Class<? extends com.sourcebay.shared.source.MySource>) 
    [ERROR] Errors in 'file:/home/santaris/workspace/SourceBay/src/com/sourcebay/client/presenter/mybay/browse/CategoryBrowsePresenter.java' 
     [ERROR] Line 75: Failed to resolve 'com.sourcebay.client.model.mybay.browse.CategoryBrowseService' via deferred binding 

矛盾的是,當我通過eclipse插件運行我的應用程序時,一切正常。任何人都可以幫助我嗎?我已經檢查通過DTO解決方案解決問題,但沒有取得任何成功。此外,我試圖按照Google的建議實施CustomFieldSerializer,但沒有取得任何成功。

由於提前,

Stefanos的ANTARIS

附:對不起,巨大的柱狀:-)

回答

0

這可能是因爲在MySource.java持久性相關的註解使得implossible翻譯爲JavaScript。嘗試刪除註釋以查看它是否相關。還要確保MySource.java位於模塊xml文件(「源」元素)中聲明爲可翻譯的包中。

1

類具有持久性註釋可以在服務器端很好地工作,但如果你想將其數據傳遞給客戶,你必須創建一個普通的Java序列化類從服務器傳輸數據到客戶端。

正如前面的答案所述,客戶端不支持持久性註釋,因爲它們不能被轉換爲等效的JavaScript代碼(因爲客戶端沒有持久性的響應性,所以它是有意義的)。

+0

我同意你們兩個人關於持久性註釋,但如果我將Android對象作爲java實例傳遞,而不是作爲泛型,它的工作原理完全正常。但是,如果我沒有任何持久性註釋給我的共享類,我怎麼能將它們存儲到數據庫?我應該使用DTO解決方案嗎? – user950070

2

那麼問題是,你想在網絡上傳輸一個Class對象。我不知道爲什麼它在開發模式下工作(我已經在本地項目上嘗試過了,但失敗了),但它不應該起作用。你必須用別的東西來代替Class名字例如字符串,其中包含類的名稱。從理論上講,如果你對Class創建CustomFieldSerializer,但使用字符串而不是類會更容易它可以工作。

0

嘗試使用implements Serializable來定義您的課程。 我的意思是這樣的:

public class AndroidSource extends MySource implements Serializable{ 

public AndroidSource() { 
    super(); 
} 

} 
-1

您的RPC服務必須處理可序列化對象。域類不可轉換爲JavaScript因此,GWT不能通過網絡(RPC Protocole)發送和接收此類對象。您需要創建實現java.io.Serializable的DTO類(完成陰影域類),然後重新配置所有RPC服務以用於輸入DTO和輸出DTO。祝你的項目好運。