Jackson mapping fails for generics passed as parameter

I have a common api to get different entity from rest API. Below is a method for getting list of entities (Groovy).

class CommonRestApi<T>{

CommonRestApi(){        
}
....
List<T> getEntities(Class<T> clazz) {
    ClientResponse response = some_rest_get //works fine
    T[] entities
    if (response.status == 200) {
        try{
            GenericType<ResponseWrapper<T[]>> type = new GenericType<ResponseWrapper<T[]>>(){} //here is error

            entities = response.getEntity(type).getData()
        }catch(Exception e){
            log.debug e.getMessage()
        }
    }
    else {
        log.debug("Status Code: " + response.status)
    }
    return Arrays.asList(entities)
}
}

ResponseWrapper class (Java):

public class ResponseWrapper<T> {

private T data;

public T getData() {
    return data;
}

public void setData(T data) {
    this.data = data;
}
}

And calling the method with:

 commonRestApi.getEntities(MyDomain.class)

Here the REST api returns data successfully but the mapping to pojo does not work. The error message is just: null. Can anybody tell me if it is possible at all. If yes please give me some guidance. Note: The common API class is in groovy

Answers


I think the issue is that you can't instantiate something with generics

new GenericType<ResponseWrapper<T[]>>(){}

fails for the same reason that

new T[10];

would fail. The type T is completely erased by the compiler, and does not exist at runtime.


Need Your Help

jQuery clone and trigger create

jquery clone

I'm trying to create a jQuery function to clone and append a text-field and select-box into the bottom of a div, like this:

Email Spam Filtering at the Code Level in Java

java email email-spam spam-prevention

I'm writing code to download email from various servers, some of which are outside of my control. I'd like to be able to filter out spam at the code level since I can't always rely on the servers t...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.