MULTIPART_FORM_DATA:没有为公共 javax.ws.rs.core.Response 类型的参数找到注入源

MULTIPART_FORM_DATA: No injection source found for a parameter of type public javax.ws.rs.core.Response(MULTIPART_FORM_DATA:没有为公共 javax.ws.rs.core.Response 类型的参数找到注入源)

本文介绍了MULTIPART_FORM_DATA:没有为公共 javax.ws.rs.core.Response 类型的参数找到注入源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 包 com.jerser.service;导入java.io.File;导入 java.io.FileOutputStream;导入 java.io.IOException;导入 java.io.InputStream;导入 java.io.OutputStream;导入 javax.ws.rs.Consumes;导入 javax.ws.rs.POST;导入 javax.ws.rs.Path;导入 javax.ws.rs.core.MediaType;导入 javax.ws.rs.core.Response;导入 com.sun.jersey.core.header.FormDataContentDisposition;导入 com.sun.jersey.multipart.FormDataParam;@Path("/文件上传")公共类 UploadFileService {@邮政@Path("/上传")@Consumes(MediaType.MULTIPART_FORM_DATA)公共响应上传文件(@FormDataParam("file") InputStream 上传的InputStream,@FormDataParam("file") FormDataContentDisposition fileDetail) {String uploadFileLocation = "d://uploaded/" + fileDetail.getFileName();//保存writeToFile(uploadedInputStream, UploadedFileLocation);String output = "文件上传到:" + uploadFileLocation;返回 Response.status(200).entity(output).build();}//将上传的文件保存到新位置私人无效writeToFile(输入流上传的输入流,字符串上传文件位置){尝试 {输出流输出 = 新文件输出流(新文件(上传文件位置));int 读取 = 0;字节[]字节=新字节[1024];out = new FileOutputStream(new File(uploadedFileLocation));while ((read = uploadInputStream.read(bytes)) != -1) {out.write(字节,0,读取);}out.flush();out.close();} 捕捉(IOException e){e.printStackTrace();}}}

这些是我的库中的 JAR 文件:

<块引用>

aopalliance-repackaged-2.4.0-b10.jarasm-debug-all-5.0.2.jarhk2-api-2.4.0-b10.jarhk2-locator-2.4.0-b10.jarhk2-utils-2.4.0-b10.jarjavassist-3.18.1-GA.jarjavax.annotation-api-1.2.jarjavax.inject-2.4.0-b10.jarjavax.servlet-api-3.0.1.jarjavax.ws.rs-api-2.0.1.jarjaxb-api-2.2.7.jarjersey-client.jarjersey-common.jarjersey-container-servlet-core.jar球衣容器-servlet.jarjersey-core-1.11.jarjersey-guava-2.17.jar泽西-媒体-jaxb.jarjersey-multipart-1.18.jarjersey-server.jarorg.osgi.core-4.2.0.jarosgi-资源定位器-1.0.1.jar持久性-api-1.0.jar验证-api-1.1.0.Final.jar

当我尝试启动我的 tomcat 服务器时出现以下错误:

org.glassfish.jersey.server.model.ModelValidationException:应用程序资源模型的验证在应用程序初始化期间失败.[[致命] 没有找到公共 javax.ws.rs.core.Response com.jerser.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) 类型参数的注入源) 在索引 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=[]}']在 org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:528)在 org.glassfish.jersey.server.ApplicationHandler.access $500(ApplicationHandler.java:166)在 org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:327)在 org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)在 org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)在 org.glassfish.jersey.internal.Errors.process(Errors.java:315)在 org.glassfish.jersey.internal.Errors.process(Errors.java:297)在 org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)在 org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:324)在 org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:338)在 org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:171)在 org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:363)在 javax.servlet.GenericServlet.init(GenericServlet.java:160)在 org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1176)在 org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)在 org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1009)在 org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4885)在 org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5212)在 org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5207)在 java.util.concurrent.FutureTask$Sync.innerRun(未知来源)在 java.util.concurrent.FutureTask.run(未知来源)在 java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(未知来源)在 java.lang.Thread.run(未知来源)

在互联网上,我发现有很多示例展示了如何使用 RESTFul API 上传 MULTIPART 文件.但使用相同的解决方案.我也无法运行这些代码.我认为我对 JAR 文件做错了什么.谁能帮我解决这个问题?

解决方案

摆脱 jersey-multipart-1.18.jar.那是针对 Jersey 1.x 的.添加这两个

  • jersey-media-multipart-2.17
  • mimepull-1.9.3

对于 Maven,您将使用以下依赖项(您无需显式添加 mimepull 依赖项,因为这会将其拉入).

<依赖><groupId>org.glassfish.jersey.media</groupId><artifactId>jersey-media-multipart</artifactId><版本>2.17</版本><!-- 确保 Jersey 版本匹配您当前使用的那个--></依赖>

然后你需要注册MultiPartFeature.如果你使用 ResourceConfig 进行配置,你可以简单地做

register(MultiPartFeature.class);

如果您使用的是 web.xml,那么您可以将类作为 <init-param> 添加到 Jersey servlet

<init-param><param-name>jersey.config.server.provider.classnames</param-name><param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value></初始化参数>

请注意,如果您要注册多个提供程序,则可以使用逗号、分号或空格/换行符分隔每个提供程序类.您不能两次使用相同的 param-name.请参阅 Suarabh 的回答

更新

此外,一旦您摆脱了 jersey-multipart-1.18.jar,您将遇到缺少的导入类的编译错误.在大多数情况下,类名仍然相同,只是包发生了变化,即

  • org.glassfish.jersey.media.multipart.FormDataParam
  • org.glassfish.jersey.media.multipart.FormDataContentDisposition

对于 Dropwizard

如果您使用的是 Dropwizard,而不是添加 jersey-media-multipart,他们会记录让您添加 dropwizard-forms.而不是注册 MultiPartFeature,您应该注册 MultiPartBundle

@Override公共无效初始化(引导程序引导程序){bootstrap.addBundle(new MultiPartBundle());}

虽然 Dropwizard 捆绑包所做的只是将 MultiPartFeature 注册到 ResourceConfig,但实际上并没有太大区别.


一边

如果您是因为不同的 ModelValidationException 而出现在这里的,这里有一些链接可获取有关该异常的其他原因的信息.

  • 1
  • 2
  • 3

I am using Jersey based restful Service implementation strategy to build a service which will be used to upload files. My service class name is : UploadFileService.java (See Code below)

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

    }

}

These are the JAR files I have inside my lib:

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

I am getting the following error when I am trying to up my tomcat server :

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)

Over the internet I found there are plenty of example which shows How to upload MULTIPART file using RESTFul API. But with same solution. I am not able to run those code as well. I think I am doing something wrong with the JAR files. Could anyone please help me on this?

解决方案

Get rid of jersey-multipart-1.18.jar. That is for Jersey 1.x. Add these two

  • jersey-media-multipart-2.17
  • mimepull-1.9.3

For Maven you would use the following dependency (you don't need to explicitly add the mimepull dependency, as this one will pull it in).

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

Then you need to register the MultiPartFeature. If you are using a ResourceConfig for configuration, you can simply do

register(MultiPartFeature.class);

If you are using web.xml, then you can add the class as an <init-param> to the Jersey servlet

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>

Note that if you have multiple providers that you want to register, then you can delimit each provider class with a comma, semicolon, or space/newline. You cannot use this same param-name twice. See Suarabh's answer

UPDATE

Also, once you get rid of jersey-multipart-1.18.jar you will have compile errors for the missing imported classes. For the most part, the class names are still the same, just the packages have changed, i.e.

  • org.glassfish.jersey.media.multipart.FormDataParam
  • org.glassfish.jersey.media.multipart.FormDataContentDisposition

For Dropwizard

If you're using Dropwizard, instead of adding the jersey-media-multipart, they document for your to add dropwizard-forms instead. And instead of registering the MultiPartFeature, you should register the MultiPartBundle

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

Really doesn't make much difference though as all the Dropwizard bundle does is register the MultiPartFeature with the ResourceConfig.


Aside

If you are here for a different ModelValidationException, here are some links for information on other causes of the exception.

  • 1
  • 2
  • 3

这篇关于MULTIPART_FORM_DATA:没有为公共 javax.ws.rs.core.Response 类型的参数找到注入源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:MULTIPART_FORM_DATA:没有为公共 javax.ws.rs.core.Response 类型的参数找到注入源

基础教程推荐