0

When I try to do file upload in JAX-RS Jersey, I am getting an error 415 Unsupported Media Type. I have attached the code and the configuration files that I have used, could anyone help me with this, please.I also tried to annote the class with @MultipartConfig. I am not passing any header in Postman.

Remvoing FormDataContentDisposition will remove the error and file upload works but i want to know why exactyly FormDataContentDisposition doesnt work in my code

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@OAuthSecurity(enabled = false)
@Path("/upload")
public Response postForm(
    @FormDataParam("file") InputStream file,
    @FormDataParam("file") FormDataContentDisposition fileDisposition) {
    return Response.ok().build();    
}

My pom.xml

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.ibm.mfp/adapter-maven-api -->
    <dependency>
        <groupId>com.ibm.mfp</groupId>
        <artifactId>adapter-maven-api</artifactId>
        <version>8.0.2018071507</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.0.4.RELEASE</version>
        <type>jar</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.7</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-java-token-validator -->
    <dependency>
        <groupId>com.ibm.mfp</groupId>
        <artifactId>mfp-java-token-validator</artifactId>
        <version>8.0.2017020112</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.ibm.mfp/mfp-security-checks-base -->
    <dependency>
        <groupId>com.ibm.mfp</groupId>
        <artifactId>mfp-security-checks-base</artifactId>
        <version>8.0.2018030404</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>



    <!-- optional, good for handle I/O task -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.0.1</version>
    </dependency>



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

**<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.5.1</version>
    </dependency>**
</dependencies>

I have a Main class that extends Application

public class MainApplication extends Application {
    void init() throws Exception {
        //class loading intitalization
        log.info("Initialized");
    }
    
    void destroy() throws Exception {
        log.info("destroyed!");
    }
    
    String getPackageToScan() {
        // The package of this class will be scanned (recursively) to find JAX-RS resources.
        return getClass().getPackage().getName();
    }
}

there is no other configurations class added, there is no web.xml file also.

Rosen Dimov
  • 1,055
  • 12
  • 32
Swathi
  • 156
  • 1
  • 2
  • 11
  • 1
    Can you post all of your app configuration related to Jersey? Also do you see any exceptions thrown on the server side? – Paul Samsotha Aug 28 '20 at 14:11
  • I edited my answer with a few more details. It would be really helpful if you could suggest some way to resolve this – Swathi Aug 28 '20 at 14:51
  • 1
    You probably just need to [register the MultiPartFeature](https://stackoverflow.com/a/30656345/2587435) – Paul Samsotha Aug 28 '20 at 14:56
  • Tried to add class but doesnt work package com.adapter.application.configuration; import javax.ws.rs.ApplicationPath; import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.glassfish.jersey.server.ResourceConfig; import com.adapter.application.services.TestService; @ApplicationPath("") public class RaagaResourceConfig extends ResourceConfig { RaagaResourceConfig() { register( MultiPartFeature.class ); packages("com.adapter.application.services"); } } – Swathi Aug 28 '20 at 16:04
  • 1
    Can you put together a _minimal_ Github Repo that reproduces the problem? I cannot help any further If I cannot reproduce the problem. – Paul Samsotha Aug 28 '20 at 16:12
  • I don't get how you are even running a Jersey application with only those dependencies. Are you running in an EE application server? – Paul Samsotha Aug 28 '20 at 16:21

0 Answers0