DAO for fetching organizations and departments through a REST API I have the following class structure:

public class CustomDAOImpl implements CustomDAO {   

    public List&lt;Organization&gt; getOrganizations() {
        List&lt;Organization&gt; results = new ArrayList&lt;Organization&gt;();
        Client client = ClientHelper.createClient(); 
        Resources&lt;OrganizationResource&gt; response = client
                .target(restApiRoot + "organizations")
                .request(MediaType.APPLICATION_JSON)
                .get(new GenericType&lt;Resources&lt;OrganizationResource&gt;&gt;(){
                });
        List&lt;OrganizationResource&gt; resources = response.getContent();
        for (OrganizationResource resource : resources) {
            results.add(resource.getEntity());
        }
        return results;
    }

    public List&lt;Department&gt; getDepartments() {
        List&lt;Department&gt; results = new ArrayList&lt;Department&gt;();
        Client client = ClientHelper.createClient();
        Resources&lt;DepartmentResource&gt; response = client
                .target(restApiRoot + "departments")
                .request(MediaType.APPLICATION_JSON)
                .get(new GenericType&lt;Resources&lt;DepartmentResource&gt;&gt;(){
                });
        List&lt;DepartmentResource&gt; resources = response.getContent();
        for (DepartmentResource resource : resources) {
            results.add(resource.getEntity());
        }
        return results;
    }
}


The abstract class :

public abstract class ResourceSupport {
    @XmlElement(name="link")
    private List&lt;Link&gt; links;
}


Entities :

public class DepartmentResource extends ResourceSupport {
    @XmlElement(name="department")
    private Department entity;

    public Department getEntity(){
        return entity;
    }
}

public class OrganizationResource extends ResourceSupport {
    @XmlElement(name="organization")
    private Organization entity;

    public Organization getEntity(){
        return entity;
    }
}


I'm new to JAVA, so I am not familiar enough with generics.

How can I improve my approach to have cleaner more elegant code?

The code must compile in JAVA 7 platform.

EDIT

I tried something like this :

private &lt;T, U&gt; List&lt;T&gt; makeRequest(Class&lt;T&gt; clazz1, Class&lt;U&gt; clazz2, String path) {
    List&lt;T&gt; results = new ArrayList&lt;T&gt;();
    Client client = ClientHelper.createClient(); 
    Resources&lt;U&gt; response = client
            .target(restApiRoot + path)
            .request(MediaType.APPLICATION_JSON)
            .get(new GenericType&lt;Resources&lt;U&gt;&gt;(){
            });
    List&lt;U&gt; resources = response.getContent();
    ...
    return results;
}


invoke like this :

List&lt;Organization&gt; list = makeRequest(Organization.class, OrganizationResource.class, "organizations");


But i have now an java.lang.ClassCastExceptionjava.lang.ClassCastException: 

Source : java.lang.reflect.Method cannot be cast to java.lang.Class
  at com.owlike.genson.reflect.TypeUtil.getTypes(TypeUtil.java:397)
  at com.owlike.genson.reflect.TypeUtil.match(TypeUtil.java:331)
  at com.owlike.genson.convert.BasicConvertersFactory.provide(BasicConvertersFactory.java:97)
  ...
  at com.owlike.genson.Genson.provideConverter(Genson.java:155)
  at com.owlike.genson.Genson.deserialize(Genson.java:387)
  at com.owlike.genson.ext.jaxrs.GensonJsonConverter.readFrom(GensonJsonConverter.java:122)
On the advise of a person from reddit here : Java code refactoring, in order to get rid of code duplication

I tried something like this :

private &lt;T0, T1 extends ResourceSupport&lt;T0&gt;&gt; List&lt;T0&gt; getResources(Class&lt;T0&gt; clazz1, Class&lt;T1&gt; clazz2, String path, GenericType&lt;Resources&lt;T1&gt;&gt; genericInstance ) {
    List&lt;T0&gt; results = new ArrayList();
    Client client = ClientHelper.createClient(); 
    Resources&lt;T1&gt; response = client
            .target(restApiRoot + path)
            .request(MediaType.APPLICATION_JSON)
            .get(genericInstance);
    List&lt;T1&gt; resources = response.getContent();
    for (T1 resource : resources) {
        results.add(resource.getEntity());
    }
    return results;
}


@XmlRootElement
@Getter
@Setter
public class OrganizationResource extends ResourceSupport&lt;Organization&gt; {

    @XmlElement(name="organization")
    private Organization entity;

    @Override
    public Organization getEntity() {
        return entity;
    }
}


@XmlRootElement
@Getter
@Setter
public abstract class ResourceSupport&lt;T&gt;  {
    @XmlElement(name="link")
    private List&lt;Link&gt; links;

    public abstract T getEntity();
}


Called like this :

GenericType&lt;Resources&lt;OrganizationResource&gt;&gt; genericInstance = new GenericType&lt;Resources&lt;OrganizationResource&gt;&gt;(){};
List&lt;Organization&gt; list = getResources(Organization.class, OrganizationResource.class, "organizations", genericInstance); 


For information, the rest api response is :

{
  "links": [],
    "content": [
        {
            "organization": {
                "id": "GES",
                "code": "GES",
                "name": "GES XX1",
                "legalNumber": "654654654"
            },
            "links": [
              {
                  "rel": "self",
                  "href": "https://server:8443/api/organizations/GES"
              },
              {
                  "rel": "organizations",
                  "href": "https://server:8443/api/organizations"
              },
            ]
        },
        {
            "organization": {
                "id": "PRO",
                "code": "PRO",
                "name": "PRO XX1",
                "legalNumber": "54545646"
            },
            "links": [ ... ]
        ...   
    ]
}