Cannot implement simple file upload in Jersey - “annotated with POST of resource, class is not recognized as valid resource method. unavailable”

ぃ、小莉子 提交于 2019-12-30 06:24:27

问题


Unable to implement simple file upload using Jersey. Missing dependency errors raised at application 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)

It seems like there is an issue with mapping the input parameters to a REST service? I have read documentation and followed several examples, and I am not deviating from those examples.

Here's the code:

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

}

I pulled in "compile 'com.sun.jersey.contribs:jersey-multipart:1.17.1'" for the FormDataParams.

EDIT: I was able to get it working in Jersey but only in this more primitive fashion:

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

This is probably a sufficient workaround, but would still like to get to the bottom of the issue.


回答1:


I had the same problem.

It was a version problem (I'd 1.8 in jersey.multipart and 1.17.1 in the rest of jersey). Setting all of them to 1.17.1 workrd for mee.

Took my answer from here:

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




回答2:


One visible problem in your code is that, you are using the same name for both the input multipart param i.e. "file"

@FormDataParam("file")

Multipart params do have an identifier, so you need to use the correct name for the second object in your method signature. Otherwise same param is going in inputstream and also for FormDataContentDisposition.



来源:https://stackoverflow.com/questions/18431012/cannot-implement-simple-file-upload-in-jersey-annotated-with-post-of-resource

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!