Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
242 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...