genericsgwtpolymorphismrequestfactoryresty-gwt

Is RestyGWT works with Generics and Polymorphic classes?


I struggle with request factory in gwt. I can make it work with Generics and Polymorphic classes. See here:

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

I think about to switch to restygwt as a transportation layer. This a new app, so there is not much to rewrite. I thought that RF will suit my needs, unfortunatelly I couldn't make it work. So I am thinking about Resty.

Please tell me would it work with generics and polymorphic classes like this below:

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

Solution

  • Yes you should not have a problem using generics with Resty. You will have to play with the Jackson annotations a bit to get them right for inheritence. Mainly the @JsonSubTypes and @JsonTypeName.

    Keep in mind that you will have to handle error handing more or less yourself. There is quite a bit more code to get the rest application working than rf but IMHO it makes your API more portable fore more platform types.

    Here is an example of what you are asking. I have used the unit tests quite often to solve some edge cases.

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