代码之家  ›  专栏  ›  技术社区  ›  user2983041

Jersey api在使用MULTIPART\u FORM\u数据时返回415不支持的媒体类型

  •  0
  • user2983041  · 技术社区  · 7 年前

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

    简单测试后api:

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

    服务器响应:

    415不支持的媒体类型

    为了测试服务器,我使用了Mozilla firefox海报插件。没有多部分的其他功能正常工作。

    enter image description here

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

    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>
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Paul Samsotha    7 年前

    使用不同的客户端,该客户端知道如何专门将文件作为多部分发送。当涉及多部分时,通常不希望手动创建请求(或设置内容类型头),因为它比普通请求稍微复杂一些。例如,这是多部分请求的外观

    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 。或者,如果您要自动化测试(在集成测试中,您可以使用 Jersey client support for multipart

        2
  •  0
  •   Simon Sadetsky    7 年前

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

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

    Content-Type: multipart/form-data
    

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