Search code examples
restweb-servicesjersey

How to resolve MessageBodyWriter not found for media type=multipart/form-data


We are trying to read a multipart REST request on server side.

The following changes have been made in a working REST Java project.

web.xml

<servlet>
        <servlet-name>AbcServlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>filter-name</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>package-name</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

dependencies.gradle

compile('com.sun.jersey.contribs:jersey-multipart:1.2')
compile('org.jvnet:mimepull:1.4')

Java code:

    @POST
    @Path("/upload")
    @Consumes("multipart/form-data")
    @Produces("application/xml")
    public Response upload(@Context UriInfo uriInfo, @Context HttpServletRequest request, FormDataMultiPart multipart ) {

.....Java code....

}

But when I test this REST service using POSTMan, I get the below error:

javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:90)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:711)
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:444)
    Truncated. see log file for complete stacktrace
Caused By: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, ...
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:106)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:86)
    Truncated. see log file for complete stacktrace
> 
Feb 04, 2019 6:22:06 PM org.glassfish.jersey.server.ServerRuntime$Responder process
SEVERE: Error occurred when processing a response created from an already mapped exception.

I also read and made the changes as per Jersey client exception: A message body writer was not found

What needs to be done to resolve this?


Solution

  • The issue was resolved by making all the jersey jars versions same.