MULTIPART_FORM_DATA:找不到类型为public javax.ws.rs.core.Response

时间:2015-06-04 19:58:13

标签: java rest jersey multipartform-data jersey-2.0

我正在使用基于Jersey的restful Service实施策略来构建将用于上传文件的服务。 我的服务类名是: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上传MULTIPART文件。但同样的解决方案。我也无法运行这些代码。 我想我在做JAR文件时出错了。有人可以帮我这个吗?

10 个答案:

答案 0 :(得分:129)

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

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

然后您需要注册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>
</init-param>

请注意,如果您要注册多个提供程序,则可以使用逗号或分号分隔每个提供程序类。您不能两次使用相同的param-name。见Suarabh's answer

<强>更新

此外,一旦摆脱jersey-multipart-1.18.jar,您将丢失导入的类的编译错误。在大多数情况下,类名仍然相同,只是包已更改,即

除了

如果你在这里寻找不同的ModelValidationException,这里有一些链接可以获得有关异常的其他原因的信息。

答案 1 :(得分:3)

我也得到了同样的例外。我在web.xml中做了以下更改

<init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.moxy.json.MoxyFeature;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
        </init-param>

并将球衣2.7改为2.9。我不知道这2的变化是什么解决了这个问题。

答案 2 :(得分:2)

以下代码对我有用:

Class->>>添加

Class属性--- >>添加


公共类userREST(){

@POST
    @Path("upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response uploadImageFile(@FormDataParam("uploadFile") InputStream fileInputStream,
            @FormDataParam("uploadFile") FormDataContentDisposition fileFormDataContentDisposition,
            @FormDataParam("FIR_REG_NUM") String FIR_REG_NUM, @FormDataParam("LOGIN_ID") String LOGIN_ID) {

        final_json_result = WriteFileInFolder.fileAnalysis(fileInputStream, fileFormDataContentDisposition, FIR_REG_NUM,
                LOGIN_ID);

        return Response.ok(final_json_result).build();

    }// uploadImageFile

公共类FileJAXRSConfig(){

package ####.jaxrs.jwt;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import ####.helper.Common@@@;
import ####.jaxrs.jwt.filters.JWTRequestFilter;
import ####.jaxrs.jwt.filters.JWTResponseFilter;
import ####.service.FileServicesREST;



@ApplicationPath("fileservice")
public class FileJAXRSConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {

        Common@@@.logging("@ApplicationPath@FileServicesREST...");
        Set<Class<?>> clazzes = new HashSet<Class<?>>();
        clazzes.add(JWTRequestFilter.class);
        clazzes.add(FileServicesREST.class);
        clazzes.add(JWTResponseFilter.class);

        return clazzes;
    }


    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("jersey.config.server.provider.packages", "####.service");
        properties.put("jersey.config.server.provider.classnames", "org.glassfish.jersey.media.multipart.MultiPartFeature");
        return properties;
    }

}

无需在web.xml中添加以下内容

<init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>mha.@@@.service</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>

答案 3 :(得分:1)

如果某人正在使用带有@FormDataParam招摇注释的@ApiOperation,那么它将无法正常工作(如此时的swagger最新版本),如下所述:

https://github.com/swagger-api/swagger-ui/issues/169

答案 4 :(得分:1)

注册MultiPartFeature。 在web.xml中,添加到Jersey servlet:

from docplex.mp.model import Model
from docplex.util.environment import get_environment

# ----------------------------------------------------------------------------
# Initialize the problem data
# ----------------------------------------------------------------------------

Categories_groups = {"Carbs": ["Meat","Milk"],"Protein":["Pasta","Bread"], "Fat": ["Oil","Butter"]}

Groups_Products = {"Meat":["Product11","Product12"], "Milk": ["Product21","Product22","Product23"], "Pasta": ["Product31","Product32"],
                   "Bread":["Product41","Product42"], "Oil":["Product51"],"Butter":["Product61","Product62"]}
Products_Prices ={"Product11":1,"Product12":4, "Product21":1,"Product22":3,"Product23":2,"Product31":4,"Product32":2,
                    "Product41":1,"Product42":3, "Product51": 1,"Product61":2,"Product62":1}




Uc={"Carbs": 1,"Protein":1, "Fat": 0 }

Ug = {"Meat": 0.8, "Milk": 0.2, "Pasta": 0.1, "Bread": 1, "Oil": 0.01, "Butter": 0.6}
budget=3;


def build_userbasket_model(**kwargs):


    allcategories = Categories_groups.keys()

    allgroups = Groups_Products.keys()

    allproducts = Products_Prices.keys()

    # Model
    mdl = Model(name='userbasket', **kwargs)
    z = mdl.binary_var_dict(allproducts, name='z([%s])')

    xg = {g: 1 <= mdl.sum(z[p] for p in Groups_Products[g]) for g in allgroups}

    xc = {c: 1 <= mdl.sum(xg[g] for g in Categories_groups[c]) for c in allcategories}


    mdl.add_constraint(mdl.sum(Products_Prices[p] * z[p] for p in allproducts) <= budget)
    mdl.maximize(mdl.sum(Uc[c] * xc[c] for c in allcategories) + mdl.sum(
        xg[g] * Uc[c] * Ug[g] for c in allcategories for g in Categories_groups[c]))
    mdl.solve()



    return mdl

if __name__ == '__main__':
    """DOcplexcloud credentials can be specified with url and api_key in the code block below.

    Alternatively, Context.make_default_context() searches the PYTHONPATH for
    the following files:

        * cplex_config.py
        * cplex_config_<hostname>.py
        * docloud_config.py (must only contain context.solver.docloud configuration)

    These files contain the credentials and other properties. For example,
    something similar to::

       context.solver.docloud.url = "https://docloud.service.com/job_manager/rest/v1"
       context.solver.docloud.key = "example api_key"
    """
    url = None
    key = None

    mdl = build_userbasket_model()

    # will use IBM Decision Optimization on cloud.
    if not mdl.solve(url=url, key=key):
        print("*** Problem has no solution")
    else:
        mdl.float_precision = 3
        print("* model solved as function:")
        mdl.print_solution()

        # Save the CPLEX solution as "solution.json" program output
        with get_environment().get_output_stream("solution.json") as fp:
            mdl.solution.export(fp, "json")

答案 5 :(得分:0)

我在Scala中遇到了同样的问题,这帮助我解决了这个问题。只是想添加一些Scala特定的详细信息,以帮助将Dropwizard与Scala一起使用的任何人。这是一个如何在Scala和Dropwizard项目中“注册” MultiPartFeature的示例。

package org.research.s3.service

import io.dropwizard.Application
import io.dropwizard.setup.Environment
import org.research.s3.service.resource._
import org.research.service.s3.resource.UploadResource

import org.glassfish.jersey.media.multipart.{FormDataParam,MultiPartFeature}


class CmdaaApp() extends Application[CmdaaAppConfig] {



  override def run(t: CmdaaAppConfig, env: Environment): Unit = {   

    env.jersey().register(new RootResource)

    //Need this to make the file upload code work in
    env.jersey().register(new MultiPartFeature)
    env.jersey().register(new UploadResource(curBucket))


  }


}

object CmdaaApp {
  def main(args: Array[String]): Unit = new CmdaaApp().run(args: _*)
}

这是执行上传的UploadResource的代码:

package org.research.service.s3.resource

import java.io.{FileInputStream, InputStream}


import com.google.gson.{Gson, GsonBuilder}

import javax.ws.rs.core.MediaType.APPLICATION_JSON
import javax.ws.rs._
import javax.ws.rs.core.Response
import javax.ws.rs.core.MediaType
import org.research.util.OptionSerializer
import org.research.s3.service.resource.s3Bucket
import org.glassfish.jersey.media.multipart.{FormDataParam,MultiPartFeature}





@Path("/file")
class UploadResource(currentBucket: s3Bucket) {
  val gsonb = new GsonBuilder()
  gsonb.registerTypeAdapter(classOf[Option[Any]], new OptionSerializer)
  val gson = gsonb.create


  @POST
  @Path("upload")
  @Produces(Array(APPLICATION_JSON))
  @Consumes(Array(MediaType.MULTIPART_FORM_DATA))
 // def uploadFile(): Response = {
  def uploadFile(@FormDataParam("file")  uploadedInputStream: InputStream): Response = {

    /* Need code here to get a uuid for the file name
       Then return the uuid if we have success and of course 200
     */

       Response.ok.entity(currentBucket.upload("testName",uploadedInputStream,false)).build()
    //Response.ok().build()
  }

}

此代码引用了s3存储桶,但您不需要它。您只需用代码替换该调用,即可将传入的文件数据下载到常规文件中。

答案 6 :(得分:0)

此异常错误的另一个可能原因是,当在参数上声明多个工厂时,Jersey只搜索与最后一个注释关联的工厂。 (请参见bug report

在解决此问题之前,如果您使用的是BL以外的其他注释,则必须排在最后。

这有效:

@FormDataParam

这不是:

@NotEmpty @FormDataParam("myParam") String myParam

答案 7 :(得分:0)

当我尝试上传文件时,我遇到了同样的问题。 我花了很多时间才找到解决问题的方法。

1。如果您更改了JAR文件的版本,则可能存在版本冲突!

  

清理工件/库并重建项目。

2。您还需要注册您的UploadFileService类:

register(MultiPartFeature.class);
register(UploadFileService.class);

希望它会帮助某人并节省您的时间。

答案 8 :(得分:0)

以防将来有人遇到此问题并遇到与我遇到的相同问题。确保您导入的注释来自正确的包。异常不会告诉您哪个参数有问题,它可能是任何一个参数。就我而言,我导入的是 javax.websocket.server.PathParam 而不是 javax.ws.rs.PathParam

答案 9 :(得分:0)

如果您在为上传资源编写 Dropwizard 测试时遇到此错误,这是解决方案:

  1. 添加对 dropwizard-forms 的依赖

2.在应用文件中添加:

    @Override
    public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
        bootstrap.addBundle(new MultiPartBundle());
    }
  1. 在测试文件中添加:
    ResourceExtension.builder()
                .addResource(new FileResource())
                .addProvider(new MultiPartFeature())
                .build();