【问题标题】:Is RestyGWT works with Generics and Polymorphic classes?RestyGWT 是否适用于泛型和多态类?
【发布时间】:2015-12-01 13:12:10
【问题描述】:

我在 gwt 中处理请求工厂。我可以让它与泛型和多态类一起工作。见这里:

RequestFactory client-side inheritance of typed class with generics

Gwt Request Factory. Generics and Inheritance on client side

RequestFactory Entity's parameter: List<OfOtherEntity> is null on client. On server is ok

我考虑改用 restygwt 作为传输层。 这是一个新的应用程序,所以没有太多要重写的。 我认为 RF 会满足我的需要,不幸的是我无法让它工作。所以我在考虑Resty。

请告诉我它是否适用于下面这样的泛型和多态类:

@MappedSuperclass
public  class GenericModel<T extends GenericModel<T>> implements Identifiable, Versionable {
    getId();
    getVersion();
}

@MappedSuperclass
public abstract class GenericDictionaryModel<T extends GenericModel<T>> extends GenericModel<T> {        getName();
    getActive();
}

@Entity class VocabularyDictionaryModel extends GenericDictionaryModel {
    someCustomeMethod();
}


public class GenericDao<T extends GenericModel<T>> {
    public List<GenericModel<T>> getList() {
        return JPA.em().createQuery("FROM " + entityClass.getSimpleName()).getResultList();
    }
}



public class GenericDictionaryDao<T extends GenericDictionaryModel<T>> extends GenericDao<T>{
    public List<GenericDictionaryModel<T>> getListOrderedByName() {
        try {
            return JPA.em()
                      .createQuery("FROM " + entityClass.getSimpleName() + " ORDER BY name")
                      .getResultList();
        } catch (ClassCastException e) {
            return new LinkedList<GenericDictionaryModel<T>>();
        }
    }
}

【问题讨论】:

  • 出于好奇,您在使用 RF 时还有什么问题?有错误吗?通过查看您的其他问题,我感觉您遇到了一个问题,即除非在子实体类型的 RequestContext 中声明该方法,否则无法调用该方法。这意味着如果您扩展 GenericContext,则必须在子 RequestContext 接口中再次添加该方法。这是额外的工作,但是当我想让代码干净时,它允许我将 GenericContext 用作
  • 我想避免代码重复。我很震惊通用不能开箱即用。我仍然在与 RF 斗争。

标签: generics gwt polymorphism requestfactory resty-gwt


【解决方案1】:

是的,在 Resty 中使用泛型应该没有问题。您将不得不稍微使用 Jackson 注释以使它们正确继承。主要是@JsonSubTypes@JsonTypeName

请记住,您将不得不或多或少地自己处理错误处理。与 rf 相比,让其余应用程序工作的代码要多得多,但恕我直言,它使您的 API 在更多平台类型之前更具可移植性。

这是您所要求的示例。我经常使用单元测试来解决一些极端情况。

https://github.com/resty-gwt/resty-gwt/blob/master/restygwt/src/test/java/org/fusesource/restygwt/client/basic/ParameterizedTypeDTO.java

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2012-08-27
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多