Shiridish
Shiridish

Reputation: 4962

415 Unsupported Media Type error when uploading file to Webservice through Postman

I have used Jersey Restful API to create a web service and I have the below:

@POST
@Path("/process/")
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Produces({MediaType.APPLICATION_JSON})
public Response process(@FormDataParam("upload") InputStream is, @FormDataParam("upload") FormDataContentDisposition formData);

I have used the following dependencies:

   <dependency>
       <groupId>javax.ws.rs</groupId>
       <artifactId>javax.ws.rs-api</artifactId>
       <version>2.1-m01</version>
   </dependency>

    <!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart -->
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.8</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.ws.rs/jsr311-api -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>

Configuration in Web.xml:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/ws-context.xml</param-value>
    </context-param>

in ws-context.xml, I have this part:

<bean id="restManagerService" class="com.rs.service.impl.RestManagerServiceImpl">
        <property name="restRequestService" ref="restRequestService" />
    </bean>

    <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000" />
    </bean>

    <jaxrs:server id="userManagerREST" address="/rest/v1">
        <jaxrs:serviceBeans>
            <ref bean="restManagerService" />
        </jaxrs:serviceBeans>

        <jaxrs:providers>
            <ref bean='jsonProvider' />
            <ref bean='multipartResolver' />
            <bean class="com.rs.exception.ExceptionHandler" />
        </jaxrs:providers>
    </jaxrs:server>

Now to test this, I am using Postman app to send a Post request. Below is the content from the code window:

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryxxxxxxxxx

------WebKitFormBoundaryxxxxxxxxx
Content-Disposition: form-data; name="upload"; filename="test.json"    

I have already referred to several samples on google, like this , this, and this and I see that I have provided the parameters correctly but I still get 415 Unsupported Media Type error in Postman. I have several other web services in this project which consumes MediaType application/json so the project configuration shouldn't be an issue.

Can somebody please shed some light as to what is wrong here.

UPDATE: Added additional details related to all jersey and WS related dependencies used and important content from the web.xml file

Upvotes: 2

Views: 15763

Answers (1)

Esteban Rincon
Esteban Rincon

Reputation: 2110

It could be a problem in Postman, not your code. I had this problem as well.

Try removing Content-type header, and make sure you select form-data in the body tab:

enter image description here

enter image description here

Upvotes: 10

Related Questions