user2983041
user2983041

Reputation: 1861

Jersey api return 415 unsupported media type when using MULTIPART_FORM_DATA

In server start multipart feature is registered:

public static HttpServer startServer() {

    final ResourceConfig rc = new ResourceConfig().packages("com.server.rest");

    rc.register(MultiPartFeature.class);

    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}

Simple test POST api:

@POST
@Path("/user-picture")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String uploadFile(FormDataMultiPart data) {


    return "OK";

}

Server response:

415 unsupported media type

To test server I use Mozilla firefox Poster pluging. Other funcions without multipart work ok.

enter image description here

I tested with diferent content type like images with same result.

Jersey version is 2.17

pom.xml multipart dependency:

 <dependency>
     <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId> 
</dependency>

Full pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example.rest</groupId>
    <artifactId>jersey-service</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>jersey-service</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.server.rest.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <jersey.version>2.17</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Upvotes: 0

Views: 1759

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 208944

Use a different client that knows how to specifically send files as multipart. You generally don't want to manually create the request (or set the Content-Type header) when it comes to multipart as the it is a little more complicated than a normal request. For example, this a what a multipart request looks like

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

--AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--AaB03x--

See more a W3c

One client you can use is Postman. Or if you are going to automate the test (in an integration test, you can use the Jersey client support for multipart

Upvotes: 1

Simon Sadetsky
Simon Sadetsky

Reputation: 544

The POST method handler is annotated with @Consumes(MediaType.MULTIPART_FORM_DATA).

So, to be mapped to it your request should contain content of the same type. Check that your request header contains

Content-Type: multipart/form-data

Also, I'm not sure about the method argument you use.

Upvotes: 0

Related Questions