Search code examples
exceptionmultipartjersey-2.0

Jersey: How to register MultiPartConfigProvider class


I'm trying to load multiple files with jersey, but it demands me some configuration that i can't find. This is the dependency, I had to add it because jersey couldn't resolver the type "multipart/related":

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.7</version>
 </dependency>

This is the API where I use the multipart:

    @PUT
    @Consumes("multipart/related")
    @Path("/{id}/user/{userId}")
    public Response loadFile(@PathParam("id") int id, @PathParam("userId") int userId, MultiPart files) throws Exception{
        int resultado = tool.loadFile(id, userId, files);
        switch (resultado){
        case 401:   return Response.status(401).entity("No existe el experimento con id: "+id).build();
        case 402:   return Response.status(402).entity("El tipo de uno de los archivos no se encuentra en los tipos de entregables del experimento").build();
        case 403:   return Response.status(403).entity("El usuario: "+userId+" no participa en el experimento").build();
        case 405:   return Response.status(405).entity("Debe ingresar un comentario").build();
        case 406:   return Response.status(406).entity("Hay entregables sin subir").build();
        case 200:   return Response.status(200).build();
        }
        return null;
    }

This is the web.xml, I think I am missing something here:

    <?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <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>org.mockapi</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

And this is the exception:

A MultiException has 3 exceptions.  They are:
1. java.lang.IllegalArgumentException: The MultiPartConfig instance we expected is not present.  Have you registered the MultiPartConfigProvider class?
2. java.lang.IllegalStateException: Unable to perform operation: create on com.sun.jersey.multipart.impl.MultiPartReaderClientSide
3. java.lang.IllegalStateException: Unable to perform operation: create on org.glassfish.jersey.message.internal.MessageBodyFactory

Solution

  • If we are using new version of dropwizard, then the dependency should be, <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.15</version> </dependency> Because new version of dropwizard uses org.glassfish.jersey.core:jersey-server:jar.
    And we have to register MultiPartFeature in our Application's run method as environment.jersey().register(MultiPartFeature.class);
    For more information you can check here