1

http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/ I'm following this guide and run to a problem. I have some questions.

  1. Do all the dependency have to be corresponded? My project has some org.glassfish.jersey dependency and this guide suggest to use org.sun.jersey. Do I have to change it with also the same version like this?

    <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.16</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.16</version>
    

  2. I have this error

    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 ***.***.****.common.dto.response.AbstractResponse ***.***.****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class ***.***.****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@90516e]}, definitionMethod=public ***.***.***.common.dto.response.AbstractResponse ***.***.*****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file1, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file1, defaultValue=null]], responseType=class ***.***.***.common.dto.response.AbstractResponse}, nameBindings=[]}']
    

    This is my web service

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public AbstractResponse upload(@FormDataParam("file1") InputStream file1,
                               @FormDataParam("file1") FormDataContentDisposition filename1
                              ) {
    

    This is my call:

    $.ajax({
          url: 'http://localhost:8080/******/webapi/m2m/upload',
          data: fd,
          processData: false,
          contentType: 'multipart/form-data',
          type: 'POST',
          success: function(data){
            alert(JSON.stringify(data));
            return;
          }
        });
    

The web service is reachable if it only has 1 parameter (FormData InputStream). How to fix it?

  1. I also want to add another String parameter for the web service. What should I do?

Thank you peeskillet for the answers. A bit extra.

SEVERE: The web application [/linterm2m] created a ThreadLocal with key of type [org.jvnet.hk2.internal.PerLocatorUtilities$1] (value [org.jvnet.hk2.internal.PerLocatorUtilities$1@df94b1]) and a value of type [java.util.WeakHashMap] (value [{}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
dtxd
  • 49
  • 2
  • 11

1 Answers1

5

If you project is using org.glassfish, you are using Jersey 2. com.sun is Jersey 1, and you should never mix the two. That being said, the error you are facing is most likely due to the fact that you didn't register the MultipartFeature. When the resource model (the resource methods) are being validated for "correctness" at startup, if the feature is not registered, the annotations specific to this feature is unknown, and it's like just having no annotation. And you cant have more than one method param with no annotation.

If you are using a ResourceConfig, you can simply use

public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        register(MultiPartFeature.class);
    }
}

If you are using web.xml, then can set an <init-param> for the Jersey servlet you registered

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

"I also want to add another String parameter for the web service. What should I do?"

You will need to make that as part of the multipart request and the client needs to make sure to send it as part of the multipart also. On the server side just add another @FormDataParam("anotherString") String anotherString as a method parameter. As for the client, I don't know jQuery that will to help with that. Haven't tested, but you can try this, which shows data being appended to FormParam. Here's something with Angular, where I built the request body myself. Might be a little much, as you may not need to explicitly set the content types.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thank you for the web.xml part. I couldn't find it anywhere else. I'm checking more things at the moment. – dtxd Apr 02 '15 at 06:59
  • It works quite well now. Thanks again for your effort. Just in case you know, may I ask about this error? I'll put it in the post SEVERE: The web application [/linterm2m] created a ThreadLocal with key of type [org.jvnet.hk2.internal.PerLocatorUtilities$1] (value [org.jvnet.hk2.internal.PerLocatorUtilities$1@df94b1]) and a value of type [java.util.WeakHashMap] (value [{}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. – dtxd Apr 02 '15 at 07:25
  • I have no idea, but one thing I was reading up on this morning was the Issue of Jersey versions in Glassfish. Most likely the Jersey version you are using is newer than the version in Glassfish. I'm not sure what version you're using, but find out what version of Jersey your Glassfish you are using ([this might help](http://blog.dejavu.sk/2014/01/21/updating-jersey-2-in-glassfish-4/)) and change the versions you are using to in your project to it. You could also try to add a `provided` to both dependencies. Not sure if this is the problem, but something you can test out – Paul Samsotha Apr 02 '15 at 07:31
  • Other than that I have no idea. I've never seen that. – Paul Samsotha Apr 02 '15 at 07:32
  • Never mind. Thank you for the lesson about jersey. Rate 10/10. – dtxd Apr 02 '15 at 07:40