【问题标题】:gwt rpc serialize generic classgwt rpc 序列化泛型类
【发布时间】:2012-09-07 17:16:08
【问题描述】:

我需要通过 gwt rpc 连接作为泛型传递一个类对象,但似乎 rpc 不与它合作。该类使用 java.io.Serializable 进行序列化。我已经使用 gwt IsSerializable 检查了它,但我仍然有错误。

这是我的代码

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 是远程服务模型

@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);

}

调用rpc的CategoryBrowsePresenter.java

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,但也没有任何成功。

提前致谢,

斯特凡诺斯·安塔里斯

附:对不起,这个巨大的帖子:-)

【问题讨论】:

    标签: gwt rpc gwt-rpc


    【解决方案1】:

    问题在于您试图通过网络传输Class 对象。我不知道为什么它在开发模式下工作(我已经在本地项目上尝试过但失败了),但它不应该工作。您必须使用其他名称而不是 Class 名称,例如 String,它将包含类名称。如果为Class 创建CustomFieldSerializer,理论上它可以工作,但使用String 代替Class 会更容易。

    【讨论】:

      【解决方案2】:

      具有持久性注解的类可以在服务器端正常工作,但如果要将其数据传递给客户端,则必须创建一个普通的 java 可序列化类来将数据从服务器传输到客户端。

      如上一个答案所述,客户端不支持持久性注释,因为它们无法转换为等效的 javascript 代码(这是有道理的,因为客户端没有持久性的责任)。

      【讨论】:

      • 我同意你们两个关于持久性注释的观点,但如果我将 Android 对象作为 java 实例而不是作为泛型传递,它完全可以正常工作。但是,如果我的共享类没有任何持久性注释,我如何将它们存储到数据库中?我应该使用 DTO 解决方案吗???
      【解决方案3】:

      可能是 MySource.java 中与持久性相关的注释使其无法转换为 javascript。尝试删除注释以查看它是否相关。还要确保 MySource.java 位于模块 xml 文件(“源”元素)中声明为可翻译的包中。

      【讨论】:

        【解决方案4】:

        尝试使用implements Serializable 来定义你的类。 我的意思是这样的:

            public class AndroidSource extends MySource implements Serializable{
        
        public AndroidSource() {
            super();
        }
        
        }
        

        【讨论】:

          【解决方案5】:

          您的 RPC 服务必须只处理可序列化对象。域类无法转换为 JavaScript,因此 GWT 无法通过网络(RPC 协议)发送和接收此类对象。您需要创建实现 java.io.Serializable 的 DTO 类(至尊阴影域类),然后重新配置所有 RPC 服务以在输入 DTO 和输出 DTO 中使用。祝你的项目好运。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-04-29
            • 1970-01-01
            • 1970-01-01
            • 2012-04-18
            • 2012-03-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多