RESTEasy client that should use generics I have written some code using RESTEasy to handle RESTful service calls and parse the response XML into relevant JAXB annotated classes. At the moment I have seperate methods for each return type:

public class RestBase {
    protected final RestClient restClient;

    public RestBase() {
        restClient = RestManager.getClient();
    }

    protected List&lt;GridNodeDTO&gt; getGridNodes(String path, List&lt;String[]&gt; params){
        Response entity = getEntity(path, params);
        return entity.readEntity(new GenericType&lt;List&lt;GridNodeDTO&gt;&gt;() {});
    }

    protected UnauthorizedAgentDTO getAgent(String path, List&lt;String[]&gt; params){
        Response entity = getEntity(path, params);
        return entity.readEntity(new GenericType&lt;UnauthorizedAgentDTO&gt;() {});
    }

    // other types skipped ...

    protected Response getEntity(String path, List&lt;String[]&gt; params) {
        ResteasyWebTarget target = getTarget();
        target = target.path(path);
        if (params != null) {
            for (String[] s : params) {
                target = target.queryParam(s[0], s[1]);
            }
        }

        Response response = target.request(MediaType.APPLICATION_XML).get();
        if (response.getStatus() != Response.Status.OK.getStatusCode()) {
            throw new RuntimeException(String.format("Invalid response status: %d\n%s", response.getStatus(), response));
        }
        return response;
    }

    private ResteasyWebTarget getTarget() {
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target(UriBuilder.fromUri(PropertiesHandler.getPropertyByKey("restUrl")).build());
        client.register(new AddAuthHeadersRequestFilter(restClient.getUser(), restClient.getPass()));
        return target;
    }


Could I reduce the amount of code by using generics? I have had some luck with using:

@SuppressWarnings("unchecked")
protected &lt;T&gt; T getRestResponse(String path, List&lt;String[]&gt; params, final Class&lt;T&gt; type) {
    // skipped ... getResponse()
    return (T)response.readEntity(new GenericType(type) {});
}


But this does not work with lists, because it seems, that the program knows nothing of generic type of list at runtime. And it seems that the compiler also kind of knows it (type erasure). 
What ways there are to optimize my routines?
I managed to get RESTEasy to work with generics (both lists and single objects) and parse the response to JAXB annotated classes by using the following code that uses reflection:

&lt;T&gt; List&lt;T&gt; getList(String path, List&lt;String[]&gt; params, final Class&lt;T&gt; clazz) {
    GenericType&lt;List&lt;T&gt;&gt; type = getListType(clazz);
    Response entity = getEntity(path, params);
    return entity.readEntity(type);
}

&lt;T&gt; T getObject(String path, List&lt;String[]&gt; params, final Class&lt;T&gt; type) {
    Response entity = getEntity(path, params);
    return (T)entity.readEntity(new GenericType(type) {});
}

private &lt;T&gt; GenericType&lt;List&lt;T&gt;&gt; getListType(final Class&lt;T&gt; clazz) {
    ParameterizedType genericType = new ParameterizedType() {
        public Type[] getActualTypeArguments() {
            return new Type[]{clazz};
        }

        public Type getRawType() {
            return List.class;
        }

        public Type getOwnerType() {
            return List.class;
        }
    };
    return new GenericType&lt;List&lt;T&gt;&gt;(genericType) { };
}