Search code examples
mavenjerseyglassfishjersey-2.0multipart

No injection source found for Multipart feature


when I try to deploy my maven project to Glassfish 5, I get the following error:

[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.test.Resources.AccountResource.addProfilePicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.test.Resources.AccountResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1efcbc1f]}, definitionMethod=public javax.ws.rs.core.Response com.test.Resources.AccountResource.addProfilePicture(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=profilePicture, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=profilePicture, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']. Please see server.log for more details.

This is the code which causes the troubles:

@POST
@Path("addProfilePicture")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addProfilePicture(@FormDataParam("profilePicture") InputStream pic,
                                  @FormDataParam("profilePicture") FormDataContentDisposition formDataContentDisposition){



    return Response.ok().build();
}

my web.xml

<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.test</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

According to similar answers, I should add the maven dependency and register the MultiPartFeature.

As you can see, I did both.

@ApplicationPath("/")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {

        HashSet<Class<?>> classes = new HashSet<>();
        classes.add(MultiPartFeature.class);
        System.out.println("added multipart feature");
        classes.add(AccountResource.class);
        return classes;
    }
}

and here are my maven dependencies:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.11</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>${jersey.version}</version>
    </dependency>
</dependencies>

I already tried to change the scope to provided and used ResourceConfig instead of Application.


Solution

  • So here's the thing. You have two different application configurations: the web.xml and the ApplicationConfig class. These are to completely separate configurations, which are both valid all by themselves. The ApplicationConfig class is the one that registers the MultiPartFeature, but it seems like the configuration that is being used is the web.xml. I never really tested what happens when you have two different application configurations, but it seems like your web.xml is taking precedence. And in your web.xml, you are not configuring the MultiPartFeature.

    If you want to just use the web.xml, then you can check out this answer for how out can configure it.

    You can however just delete the entire web.xml. This will cause the ApplicationConfig to kick it. But notice the two differences: the @ApplicationPath on the ApplicationConfig acts like the url-mapping in the web.xml. So if you delete the web.xml, then the base path will just be / (or nothing) instead of the /webapi like you have in the web.xml

    If you decide the delete the web.xml, I suggest you use ResourceConfig instead of Application. You can get some good information about this class and different configuration possibilities in this post from What exactly is the ResourceConfig class in Jersey 2?