No available MessageBodyWriter for media type "multipart/form-data"
Asked Answered
N

2

1

I'm trying to create a jersey client to hit a server with multipart request.
I have the file to upload as a java.io.ByteArrayOutputStream type stream.
I searched for the error and came across this question here and this answer here
and added register(MultiPartFeature.class) while creating a Client, but to no effect.
I probably just need a nudge in the right direction. Thanks!

I'm not using Maven or anything.

Relevant part from my java file is:

FormDataMultiPart multiPart = new FormDataMultiPart();
FormDataContentDisposition.FormDataContentDispositionBuilder dispositionBuilder = FormDataContentDisposition
 .name("file");

dispositionBuilder.fileName("file.zip");
FormDataContentDisposition formDataContentDisposition = dispositionBuilder.build();

/*
 * byteArrayOutputStream is of type java.io.ByteArrayOutputStream
 * and contains the file I wish to upload.
 */
multiPart.bodyPart(
 new FormDataBodyPart("file", byteArrayOutputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE)
 .contentDisposition(formDataContentDisposition));

Entity < FormDataMultiPart > entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);

Client client = ClientBuilder.newClient(new ClientConfig().register(MultiPartFeature.class));
WebTarget webTarget = client.target("http://localhost:3000/path/to/service");

// send request

Invocation.Builder invocationBuilder = webTarget.request();
Response response = invocationBuilder.post(entity);
return response;

Relevant part from web.xml is:

          <servlet>
              <servlet-name>Jersey Web Application</servlet-name>
              <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                     <param-name>jersey.config.server.provider.packages</param-name>
                     <param-value>com.example</param-value>
              </init-param>
              <init-param>
                     <param-name>jersey.config.server.provider.classnames</param-name>
                     <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
       </servlet>

I get this following error while trying to run it inside Eclipse on a Apache Tomcat 8.5 runtime server.

SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/TestRest] threw exception [javax.ws.rs.ProcessingException: No available MessageBodyWriter for class "class java.io.ByteArrayOutputStream" and media type "multipart/form-data".] with root cause
java.lang.IllegalArgumentException: No available MessageBodyWriter for class "class java.io.ByteArrayOutputStream" and media type "multipart/form-data".
    at org.glassfish.jersey.media.multipart.internal.MultiPartWriter.writeTo(MultiPartWriter.java:229)
    at org.glassfish.jersey.media.multipart.internal.MultiPartWriter.writeTo(MultiPartWriter.java:79)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
Novokuznetsk answered 14/3, 2017 at 19:5 Comment(2)
I think you need to use an InputStream (or byte[]), not an OutputStream. You might as well use the InputStream though so you don't use up memoryWoo
@peeskillet Converting the code to utilize InputStream (ByteArrayInputStream) works for me! Can you please add it as an answer? Cheers!Novokuznetsk
W
1

There's no MessageBodyWriter that handles OutputStream. Use an InputStream instead

Woo answered 14/3, 2017 at 19:29 Comment(0)
A
0

Ok , you register MultiPartFeature.class

Client client = ClientBuilder.newClient(new ClientConfig().register(MultiPartFeature.class));

but you have to handle and register response type, Suppose you upload a file and response's media type is json, so you must add a register like that;

Client client = ClientBuilder.newClient(new ClientConfig().register(MultiPartFeature.class).register(JacksonFeature.class));

Amieva answered 27/11, 2018 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.