1
\$\begingroup\$

I have the following class structure:

public class CustomDAOImpl implements CustomDAO {   

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

    public List<Department> getDepartments() {
        List<Department> results = new ArrayList<Department>();
        Client client = ClientHelper.createClient();
        Resources<DepartmentResource> response = client
                .target(restApiRoot + "departments")
                .request(MediaType.APPLICATION_JSON)
                .get(new GenericType<Resources<DepartmentResource>>(){
                });
        List<DepartmentResource> 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<Link> 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 <T, U> List<T> makeRequest(Class<T> clazz1, Class<U> clazz2, String path) {
    List<T> results = new ArrayList<T>();
    Client client = ClientHelper.createClient(); 
    Resources<U> response = client
            .target(restApiRoot + path)
            .request(MediaType.APPLICATION_JSON)
            .get(new GenericType<Resources<U>>(){
            });
    List<U> resources = response.getContent();
    ...
    return results;
}

invoke like this :

List<Organization> 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)
\$\endgroup\$
0
\$\begingroup\$

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 <T0, T1 extends ResourceSupport<T0>> List<T0> getResources(Class<T0> clazz1, Class<T1> clazz2, String path, GenericType<Resources<T1>> genericInstance ) {
    List<T0> results = new ArrayList();
    Client client = ClientHelper.createClient(); 
    Resources<T1> response = client
            .target(restApiRoot + path)
            .request(MediaType.APPLICATION_JSON)
            .get(genericInstance);
    List<T1> resources = response.getContent();
    for (T1 resource : resources) {
        results.add(resource.getEntity());
    }
    return results;
}


@XmlRootElement
@Getter
@Setter
public class OrganizationResource extends ResourceSupport<Organization> {

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

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


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

    public abstract T getEntity();
}

Called like this :

GenericType<Resources<OrganizationResource>> genericInstance = new GenericType<Resources<OrganizationResource>>(){};
List<Organization> 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": [ ... ]
        ...   
    ]
}
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.