0

Trying to use the Multipart-Feature for my Jersey 3 Web app. Unfortunately, it seems that there are no injection sources found...

[[FATAL] No injection source found for a parameter of type public jakarta.ws.rs.core.Response com.mycompany.file.DataController.uploadDataFile(java.lang.Long,java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition,org.glassfish.jersey.media.multipart.FormDataBodyPart) throws java.sql.SQLException at index 1.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.mycompany.file.data.DataController, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@64fe3f7f]}, definitionMethod=public jakarta.ws.rs.core.Response com.mycompany.file.DataController.uploadDataFile(java.lang.Long,java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition,org.glassfish.jersey.media.multipart.FormDataBodyPart) throws java.sql.SQLException, parameters=[Parameter [type=class java.lang.Long, source=dataId, defaultValue=null], Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataBodyPart, source=file, defaultValue=null]], responseType=class jakarta.ws.rs.core.Response}, nameBindings=[]}']

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>app</artifactId>
    <groupId>com.mycompany.app</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>app</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>11</java.version>
        <jersey.version>3.0.1</jersey.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <repositories>
        <repository>
            <id>mavenCentral</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>jakarta.platform</groupId>
            <artifactId>jakarta.jakartaee-web-api</artifactId>
            <version>9.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.persistence</groupId>
            <artifactId>jakarta.persistence-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.11.3</version>
        </dependency>
    </dependencies>
</project>

web.xml

<servlet>
    <servlet-name>File Upload REST Endpoint</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.mycompany.file</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Endpoint

import jakarta.ws.rs.*;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

    @Path("/api")
    public class DataController {

    @Context
    ServletContext context;

    private DataSource ds;
    private DataService service;

    @PostConstruct
    public void init() {
        ds = (DataSource) context.getAttribute(MDD_DATASOURCE_CONTEXT_NAME);
        service = new DataService(ds);
    }

    @POST
    @Path("/data/{dataId}/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadDataFile(@PathParam("dataId") Long dataId,
                                           @FormDataParam("file") InputStream inputStream,
                                           @FormDataParam("file") FormDataContentDisposition fileMetaData,
                                           @FormDataParam("file") FormDataBodyPart body)
            throws SQLException {
        service.handleDataFile(dataId, inputStream, fileMetaData);
        return Response.ok().build();
    }
}

I've been looking at other posts, in my opinion I have the correct dependency and I also register the Multipart-Feature in my web.xml. What am I missing?

Thanks for any inputs!

Remo
  • 1,112
  • 2
  • 12
  • 25
  • 1
    What is the import for @FormDataParam? Can you show your pom.xml (dependencies) – Paul Samsotha Apr 03 '21 at 17:43
  • 1
    Are you [using the 'classnames' init-param more than once](https://stackoverflow.com/q/66932475/2587435). You can only use it once. If you need to register more than one class, then delimit them with either a comma, semicolon, or new line. – Paul Samsotha Apr 03 '21 at 17:47
  • @PaulSamsotha I updated the question with the pom.xml as well as the imports. What does using the 'classnames' init param more than once mean exactly? I have it the same way as posted in my question you've linked. Does this mean, I should declare one servlet in the web.xml and register the other Endpoints/Resources through package-scanning in that servlet, in order to register the Multipart-Feature correctly? – Remo Apr 03 '21 at 21:49
  • 1
    What is the PathParam import? – Paul Samsotha Apr 04 '21 at 05:43
  • I import it through jakarta.ws.rs.*; – Remo Apr 04 '21 at 10:24
  • 1
    Start a new project with _just_ what you've posted here and I'm pretty sure you won't get this error. Nothing looks wrong with what you've posted, given all the your comments are correct. – Paul Samsotha Apr 04 '21 at 16:44
  • 1
    One thing I caught that looks weird is the fact that you have _two_ parameters for the same body part. The `@FormDataParam("value")` is the name of the body part. The body part you are trying to extract is named `"file"`. You have both an InputStream parameter _and_ a FormDataBodyPart parameter. This is redundant and I am not even sure if it's allowed (I never tried it). You should only have _one_ entity parameter for each part (along with a FormDataContentDisposition). If you use FormDataBodyPart, there is no need for a FormDataContentDisposition as you can get it from the FormDataBodyPart. – Paul Samsotha Apr 04 '21 at 20:41
  • @PaulSamsotha thx to you, the hint with the using classnames init param more than once was correct. Changed the definition in my web.xml. – Remo Apr 05 '21 at 09:50

1 Answers1

0

Solution is to only call the 'classnames' init-param only once. Thx to Paul Samsotha.

For this, I changed the web.xml configuration, it looks now like this:

web.xml

 <!-- Servlets -->
    <servlet>
        <servlet-name>REST Endpoints</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>ch.mycompany</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>
                org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature
                org.glassfish.jersey.media.multipart.MultiPartFeature
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
<!-- URL-Mapping -->
<servlet-mapping>
    <servlet-name>REST Endpoints</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
Remo
  • 1,112
  • 2
  • 12
  • 25