2
votes

I am trying to implement File Upload using Jersey module in Mule.

My mule flow looks like this:

<flow name="rest-service">
    <inbound-endpoint address="http://localhost:9999/testupload"/>
    <jersey:resources>
        <component class="com.example.test.UploadFileResource"/>
    </jersey:resources>
</flow> 

If I don't put @Consumes annotation in the resource method in UploadFileResource like below, the method gets called when an HTTP Post request is made using multipart/form-data Content-type and I get HTTP 2xx status code:

@Path("/uploadfile")
public class UploadFileResource {

    @POST
    public Response uploadFile2(...) {
        logger.info("Multipart Upload");
        ...
    }
}

But when I put @Consumes annotation with MULTIPART_FORM_DATA Media Type like below, the method does not get called and I get HTTP 415 Unsupported Media type, even when the HTTP Post request is made using multipart/form-data Content-type:

@Path("/uploadfile")
public class UploadFileResource {

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile2(...) {
        logger.info("Multipart Upload");
        ...
    }
}

Any idea why 415 status comes even when @Consumes Media type matches the HTTP Post Content-type header?

1
How does the input params of #upload2() method look like?Michal Gajdos
I tried two versions: One without any input arguments, i.e. uploadFile2(), and one with two input arguments, i.e. uploadFile2(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail). I am getting the same result in both the cases.Pranav Pal
Can you try to turn on the tracing support (as described here) and post the response headers to see what's going on?Michal Gajdos
do you have the jersey-multipart JAR on your classpath?Kyle Winter

1 Answers

1
votes

You may need to register the MultipartFeature as described in the Jersey documentation, chapter 8.3.1.2 Registration.

Create a class something like this:

/**
 * 
 */
package com.verico.multipart.app;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("/")
public class MultiPartApp extends ResourceConfig {

public MultiPartApp() {
    super(MultiPartFeature.class);
    }
}

And add the following init-param to your Jersey servlet in web.xml:

     <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.verico.multipart.app.MultiPartApp</param-value>
    </init-param>