MULTIPART_FORM_DATA:未找到公共 javax.ws.rs.core.response 类型的参数的注入源

我正在使用基于泽西岛的restful服务实现策略来构建一个将用于上传文件的服务。我的服务类名称是:UploadFileService.java(请参阅下面的代码)

 package com.jerser.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("/fileUpload")
public class UploadFileService {

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

        String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();

        // save it
        writeToFile(uploadedInputStream, uploadedFileLocation);

        String output = "File uploaded to : " + uploadedFileLocation;

        return Response.status(200).entity(output).build();

    }

    // save uploaded file to new location
    private void writeToFile(InputStream uploadedInputStream,
        String uploadedFileLocation) {

        try {
            OutputStream out = new FileOutputStream(new File(
                    uploadedFileLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            out = new FileOutputStream(new File(uploadedFileLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            out.flush();
            out.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}

这些是我在lib中的JAR文件:

aopalliance-repackaged-2.4.0-b10.jar     
asm-debug-all-5.0.2.jar     
hk2-api-2.4.0-b10.jar  
hk2-locator-2.4.0-b10.jar     
hk2-utils-2.4.0-b10.jar     
javassist-3.18.1-GA.jar     
javax.annotation-api-1.2.jar     
javax.inject-2.4.0-b10.jar     
javax.servlet-api-3.0.1.jar     
javax.ws.rs-api-2.0.1.jar     
jaxb-api-2.2.7.jar     
jersey-client.jar     
jersey-common.jar     
jersey-container-servlet-core.jar     
jersey-container-servlet.jar     
jersey-core-1.11.jar     
jersey-guava-2.17.jar     
jersey-media-jaxb.jar  
jersey-multipart-1.18.jar    
jersey-server.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar     
persistence-api-1.0.jar    
validation-api-1.1.0.Final.jar

当我尝试启动我的tomcat服务器时,我收到以下错误:

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.jerser.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.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.jerser.service.UploadFileService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@d3e2d4]}, definitionMethod=public javax.ws.rs.core.Response com.jerser.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:528)
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:166)
    at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:327)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:324)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:338)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:171)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:363)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1176)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1009)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4885)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5212)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5207)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

在互联网上,我发现有很多例子显示了如何使用RESTFul API上传多部分文件。但使用相同的解决方案。我也无法运行这些代码。我认为我对JAR文件做错了什么。任何人都可以帮我吗?


答案 1

摆脱 .这是泽西岛1.x。添加这两个jersey-multipart-1.18.jar

对于 Maven,您将使用以下依赖项(您不需要显式添加依赖项,因为此依赖项将提取它)。mimepull

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.17</version> <!-- Make sure the Jersey version matches
                                 the one you are currently using -->
</dependency>

然后,您需要注册 .如果您使用的是 用于配置,则只需执行MultiPartFeatureResourceConfig

register(MultiPartFeature.class);

如果您使用的是 web.xml,则可以将该类作为 a 添加到 Jersey servlet 中<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>

请注意,如果要注册多个提供程序,则可以使用逗号、分号或空格/换行符分隔每个提供程序类。您不能两次使用相同的方法。看看苏阿拉布的答案param-name

更新

此外,一旦你摆脱了,你就会有编译错误,因为缺少的导入类。在大多数情况下,类名仍然相同,只是包已更改,即jersey-multipart-1.18.jar


对于 Dropwizard

如果您使用的是 Dropwizard,而不是添加 ,而是添加 他们文档供您添加。而不是注册 ,您应该注册jersey-media-multipartdropwizard-formsMultiPartFeatureMultiPartBundle

@Override
public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
    bootstrap.addBundle(new MultiPartBundle());
}

实际上并没有太大的区别,因为所有Dropwizard捆绑包所做的就是注册.MultiPartFeatureResourceConfig


旁白

如果您在这里是为了一个不同的,这里有一些链接,以获取有关异常的其他原因的信息。ModelValidationException


答案 2

然而,这个非常通用的错误的另一个可能原因是,当在参数上声明了多个注释时,泽西岛仅搜索与最后一个注释关联的工厂。(请参阅错误报告)

在修复此问题之前,如果您使用的是 除 之外的任何其他注释,则必须排在最后。@FormDataParam

这有效:

@NotEmpty @FormDataParam("myParam") String myParam

这不会:

@FormDataParam("myParam") @NotEmpty String myParam