Java – simple file upload cannot be implemented in Jersey – “use post to annotate resources. Classes are not considered valid resource methods. Unavailable”

You can't use Jersey to upload simple files Missing dependency error raised during application boot:

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)

Does mapping input parameters to rest services seem problematic? I have read the documentation and followed a few examples, and I have not deviated from them

This is 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 introduced "compile 'com. Sun. Jersey. Contribs: Jersey multipart: 1.17.1'" for formdataparams

Editor: I can make it work in Jersey, but only in this more primitive way:

@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 may be a sufficient solution, but we still hope to find the root cause of the problem

Solution

I have the same problem

This is a version issue (I am 1.8 in Jersey. Multipart and 1.17.1 in the rest of the Jersey) Set all of these to 1.17 1 workrd for mee.

Get my answer here:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>