1

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.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Jakob Herk
  • 157
  • 11

1 Answers1

1

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?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • i deleted the web.xml and it works. Thanks Paul, I really appreciate your help :) – Jakob Herk Jul 30 '18 at 07:22
  • @JakobHerk No worries, glad to help. – Paul Samsotha Jul 30 '18 at 07:23
  • Couple other pointers: 1) All your Jersey and (Servlet) dependencies should be in a provided scope since Glassfish already has them. 2) You are using a very old version of the Servlet API, and should upgrade it. I think it should be [this one](https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api/3.1.0). But still it should be a provided scope – Paul Samsotha Jul 30 '18 at 07:26