2017-06-28 67 views
0

當在服務器啓動多要素類型註冊地:新澤西API返回415不支持的媒體使用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); 
} 

簡單測試POST API:

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


    return "OK"; 

} 

服務器響應:

415不受支持的媒體類型

要測試服務器,我使用Mozilla Firefox插件。其他funcions沒有多部分工作好。

enter image description here

我與diferent內容類型等用相同的結果的圖像進行測試。

球衣版本是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> 

回答

1

使用不同的客戶端,知道如何專門作爲多部分發送文件。當涉及多部分時,您通常不希望手動創建請求(或設置Content-Type標頭),因爲它比普通請求稍微複雜一些。例如,這是一個multipart請求是什麼樣子

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

+0

隨着Postman服務器正確處理文件 – user2983041

0

POST方法處理程序與@Consumes(MediaType.MULTIPART_FORM_DATA)註解。

因此,要映射到它,您的請求應包含相同類型的內容。檢查您的請求標頭是否包含

Content-Type: multipart/form-data 

此外,我不確定您使用的方法參數。

+0

這不會起作用。當使用multipart時,您通常不會手動設置標題,因爲您還需要在頭文件也包含在正文中,通常這會隨着你使用的任何客戶端而產生,如果你不理解你的建議,你會得到一個400錯誤的請求 –

相關問題