java - 无法在 Jersey 中实现简单文件上传 - "annotated with POST of resource, class is not recognized as valid resource method. unavailable"

标签 java rest jersey

无法使用 Jersey 实现简单的文件上传。缺少应用程序 Bootstrap 时引发的依赖项错误:

The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.foo.MyResource.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at parameter at index 0
  SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.foo.MyResource.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at parameter at index 1
  SEVERE: Method, public javax.ws.rs.core.Response com.foo.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), annotated with POST of resource, class com.foo.FS2Resource, is not recognized as valid resource method.
unavailable
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)

似乎将输入参数映射到 REST 服务有问题?我已阅读文档并遵循了几个示例,并且我没有偏离这些示例。

代码如下:

@Path("v1/")
public class FileUploadResource {


    @POST
    @Path("upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces({MediaType.APPLICATION_JSON})
    public Response uploadFile(
        @FormDataParam("file") InputStream is,
        @FormDataParam("file") FormDataContentDisposition detail) {

        String name = detail.getFileName();

        // do upload stuff
        String output = .... 

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

}

我为 FormDataParams 引入了“compile 'com.sun.jersey.contribs:jersey-multipart:1.17.1'”。

编辑:我能够让它在 Jersey 工作,但只能以这种更原始的方式:

 @POST
 @Path("upload")
 @Consumes(MediaType.MULTIPART_FORM_DATA)     
 @Produces(MediaType.TEXT_PLAIN)

 public Response uploadFile(final MimeMultipart file) {
   if (file == null) {
     return Response.status(Response.Status.BAD_REQUEST).entity("Must supply a valid file").build();

   try {
     for (int i = 0; i < file.getCount(); i++) {
       // ... do something with file.getBodyPart(i));
     }
     return Response.ok("done").build();
   } catch (final Exception e) {
     return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e).build();
   }
 }

这可能是一个足够的解决方法,但仍想深入了解问题的根源。

最佳答案

我遇到了同样的问题。

这是一个版本问题(我在 jersey.multipart 中为 1.8,在 Jersey 的其余部分为 1.17.1)。将它们全部设置为 1.17.1 workrd for mee。

从这里得到我的答案:

Missing dependency for method when doing a file upload rest web service

关于java - 无法在 Jersey 中实现简单文件上传 - "annotated with POST of resource, class is not recognized as valid resource method. unavailable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18431012/

相关文章:

java - 在 java 中使用 HashMap 的主要好处是什么?

java - http ://pubsubhubbub. appspot.com/subscribe 407 需要身份验证

Java数组: how to insert delete items in array 1D and 2D

web-services - 在 HATEOAS 中向消费者建议采取行动

java - 如何在 Jersey 服务器中获取/注入(inject)当前的 SSLSession?

java - Jetty 9.2.3 中是否存在 JDBC ConnectionPool 泄漏?

java - 在类和方法级别使用JAX-RS @Path注释的Java中的URI识别

rest - 组内的 Nexus REST API 查询工件

java - Jersey 服务器 : Return a string and a non-200 return code

jersey - Websphere 8.5 上的 JAX-RS Jersey 客户端