I'm new in java rest application. I'm trying to run an application, but i have this exception
message com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class java.util.ArrayList, and MIME media type, multipart/form-data, was not found
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, multipart/form-data, was not found
com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:155)
com.sun.jersey.api.client.Client.handle(Client.java:652)
com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570)
org.eu.paas.client.APIClient.doPost(APIClient.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
In APIClient.java:265
I have some thing like:
cr = service.path(path)
.type(MediaType.MULTIPART_FORM_DATA)
.post(ClientResponse.class, listForm);
Where listForm
is an ArrayList<InputStream>
and in the rest application I have:
@POST
@Path("{appId-appId}/action/Multideploy/env/{envId-envId}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_XML)
Response MultideployApplication(
@PathParam("appId-appId") String appid, @PathParam("envId-envId") String envid,
@FormDataParam("file") List<InputStream> uploadedInputStream);
Also in my pom.xml I have these dependencies:
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
Jersey uses
MessageBodyWriter
s to handle serialization of Java objects to the request stream (or response stream on server side, andMessageBodyReader
s to handle de-serialization of the response stream (or request stream on server side) into Java objects. You can read more at JAX-RS Entity Providers.That being said, whenever you see an error like "No MessageBodyReader(Writer) found for type so and so and java type so and so", it means that there is no serializer to handle the conversion. In you particular case, it is saying that there is not writer that can handle the conversion of an
ArrayList
to multipart/form-data. This makes sense, as how will the writer know how to make this conversion.For multipart, you need to actually use the multipart APIs on the client client side. The writer knows how to convert these API object into the required multipart parts. For the complete list of APIs, you can look at the package
com.sun.jersey.multipart
.Here is an example of sending a file
There's really no documentation for the 1.x support for Jersey multipart. But a lot of times, the best documentation is seen in the tests. So if you want some more examples to go off of, look at the tests for jersey-multipart.
UPDATE
Here is a complete test using Jersey Test Framework. Run it like any other JUnit test. Replace the constants
FILE_ONE
andFILE_TWO
with the location of some arbitrary files on your system. You should see the files saved to your current working directory (most likely the project root)UPDATE 2 (Jersey 2.x)
Here is the same test with Jersey 2.x. Just replace the constants
FILE_ONE
andFILE_TWO
with some file locations on your system, and run the test. The files should be saved to your current working directory.