1

I am trying to send a generic list to a jersey servlet using put method, using GenericEntity. I have seen a lot of examples requesting a generic list, but none of them putting it.

So the code on my server is:

@PUT
@Produces(MediaType.TEXT_HTML)
public String doPutHtml(GenericType<List<SystemInfo>> systemInfoList) {
    System.out.println(systemInfoList.toString());
    return "OK";
}

And the code on the client sending the put request:

WebResource ws;
Configuration conf = ConfigurationFactory.getConfigurationFactory()
            .getConfiguration();

Client client = Client.create();
ws = client.resource("http://" + conf.getDatacenterURL() + ":"+ conf.getDatacenterPort() + "/services/systemInfo");
GenericEntity entity = new GenericEntity<List<SystemInfo>>(systemInfoList) {};
String response = ws.accept(MediaType.TEXT_HTML).type(MediaType.APPLICATION_XML).put(String.class, entity);

When I run the client code, I get this exception:

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

So my question would be, is it possible to send a generic list this way? In case this is not possible, are there any alternatives?

Eugenio Cuevas
  • 10,858
  • 3
  • 29
  • 51

2 Answers2

2

Was receiving the exact same error through different combinations. The answer I found seems quite simple.

The client setup and call example above is correct. Specifically...

GenericEntity entity = new GenericEntity<List<SystemInfo>>(systemInfoList) {};
String response = 
ws.accept(MediaType.TEXT_HTML).type(MediaType.APPLICATION_XML).put(String.class, entity);

The GenericEntity handles the transition of the List to the restful service. Only the appropriate List is required in the service.

@PUT
@Produces(MediaType.TEXT_HTML)
public String doPutHtml(List<SystemInfo> systemInfoList) {
    System.out.println(systemInfoList.toString());
    return "OK";
}
Al Rosine
  • 31
  • 4
0

This issue is generated during marshaling and marshaling the objects.There is a similar thread here which talks about the same issue.However, that is related to JAXB.There too, it tries to serialize a list object and faces the same issue.Once you analyze the issue, it is easy to implement the same technique here as well.

Using JAXB to unmarshal/marshal a List<String>

Hope this will help you.

Community
  • 1
  • 1
UVM
  • 9,776
  • 6
  • 41
  • 66
  • Yes, but I would like jersey to do it for me. If it does it for a get, why not for a put? – Eugenio Cuevas May 02 '12 at 07:43
  • what HTTP operation you are using? – UVM May 02 '12 at 07:56
  • there is a simple unit test case for PUT operation here.http://repo.aduna-software.org/websvn/comp.php?repname=aduna&path=%2F&compare[]=%2Forg.openrdf%2Falibaba@10203&compare[]=%2Forg.openrdf%2Falibaba@10206. – UVM May 02 '12 at 08:33
  • Thanks, but it is not sending a generic ArrayList, which is my specific problem. I am able to send the request as an XML string, and then parse it in the server, but I want Jersey to handle it automatically if possible. – Eugenio Cuevas May 02 '12 at 08:44
  • Did you run that example with ArrayList and getting the same exception? – UVM May 02 '12 at 08:45
  • Thanks, it works now! I was missing `@XmlRootElement(name="List")` on the JavaBean – Eugenio Cuevas May 03 '12 at 06:12