Jersey api return 415 使用 MULTIPART_FORM_DATA 时不支持的媒体类型

Jersey api return 415 unsupported media type when using MULTIPART_FORM_DATA

在服务器启动时注册了多部分功能:

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);
}

简单测试POSTapi:

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


    return "OK";

}

服务器响应:

415 unsupported media type

测试服务器我使用Mozilla firefox Poster插件。其他没有 multipart work 的函数 ok.

我测试了不同的内容类型,比如结果相同的图像。

球衣版本为 2.17

pom.xml 多部分依赖:

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

完整 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>

POST 方法处理程序用 @Consumes(MediaType.MULTIPART_FORM_DATA) 注释。

因此,要映射到它,您的请求应包含相同类型的内容。检查您的请求 header 是否包含

Content-Type: multipart/form-data

此外,我不确定您使用的方法参数。

使用知道如何专门将文件作为多部分发送的不同客户端。当涉及到多部分时,您通常不想手动创建请求(或设置 Content-Type header),因为它比普通请求稍微复杂一些。例如,这是一个多部分请求的样子

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

您可以使用的一个客户端是 Postman. Or if you are going to automate the test (in an integration test, you can use the Jersey client support for multipart