Using genericEntity

I have several client classes that send the beans list via the PUT method to the jersey web service, so I decided to reorganize them into one class using generics. My first attempt:

public void sendAll(T list,String webresource) throws ClientHandlerException {
    WebResource ws = getWebResource(webresource);
    String response = ws.put(String.class, new GenericEntity<T>(list) {});
}

But when I called it with:

WsClient<List<SystemInfo>> genclient = new WsClient<List<SystemInfo>>();
genclient.sendAll(systemInfoList, "/services/systemInfo");

This gives me this error:

com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found

So, I tried to render the method in the GenericEntity declaration, and it works:

public void sendAll(T list,String webresource) throws ClientHandlerException {
 WebResource ws = ws = getWebResource(webresource);
 String response = ws.put(String.class, list);
}

Call using:

 WsClient<GenericEntity<List<SystemInfo>>> genclient = new WsClient<GenericEntity<List<SystemInfo>>>();
 GenericEntity<List<SystemInfo>> entity;
 entity = new GenericEntity<List<SystemInfo>>(systemInfoList) {};
 genclient.sendAll(entity, "/services/systemInfo");

So why can't I generate a generic object of a general type inside a class, but do it outside of work?

+3
source share
1 answer

The GenericEntity class is used to bypass Java type erasure. At the time the GenericEntity is instantiated, Jersey is trying to get type information.

GenericEntity list T, systemInfoList, , , . , GenericEntity , - Java .

, . ( Sun/Oracle ).

+1

All Articles