0

尝试为我的 Jersey 3 Web 应用程序使用 Multipart-Feature。不幸的是,似乎没有找到注入源......

[[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>

端点

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();
    }
}

我一直在查看其他帖子,我认为我有正确的依赖关系,并且我还在我的 web.xml 中注册了 Multipart-Feature。我错过了什么?

感谢您的任何投入!

4

1 回答 1

0

解决方案是只调用一次“类名”init-param。感谢 Paul Samsotha。

为此,我更改了 web.xml 配置,现在看起来像这样:

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>
于 2021-04-05T09:54:08.870 回答