HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

RESTEasy client that should use generics

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
clientthatgenericsresteasyshoulduse

Problem

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 getGridNodes(String path, List params){
        Response entity = getEntity(path, params);
        return entity.readEntity(new GenericType>() {});
    }

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

    // other types skipped ...

    protected Response getEntity(String path, List 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 T getRestResponse(String path, List params, final Class type) {
// skipped ... getResponse()
return (T)response.readEntity(new GenericTy

Solution

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:

 List getList(String path, List params, final Class clazz) {
    GenericType> type = getListType(clazz);
    Response entity = getEntity(path, params);
    return entity.readEntity(type);
}

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

private  GenericType> getListType(final Class 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>(genericType) { };
}

Code Snippets

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

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

private <T> GenericType<List<T>> getListType(final Class<T> 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<List<T>>(genericType) { };
}

Context

StackExchange Code Review Q#60782, answer score: 6

Revisions (0)

No revisions yet.