我正在使用jersey 2.22.1在java中构建RESTful API。我有一个资源类,我打算接收一个客户端将通过表单上传的文件。我的问题是我不断从服务器获得415不支持的媒体类型响应。有趣的是,客户端(Postman chrome扩展)收到状态0,当文件是jpg图像时连接到服务器的错误
资源类中还有另一种用于测试目的的方法,它可以正常工作。我用google搜索了stackoverflow网站,可以找到类似的情况,但没有帮助我的情况。感谢所有的帮助,提前谢谢。
资源类
package org.sime.seven.managefiles.resource;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.media.multipart.ContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
@Path("files")
public class UploadResource {
@GET
@Path("test")
@Produces(MediaType.APPLICATION_JSON)
public String test(){
return "testing 1,2,3";
}
@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(FormDataMultiPart formData){
FormDataBodyPart filePart = formData.getField("file");
ContentDisposition fileHeader = filePart.getContentDisposition();
String fileName = fileHeader.getFileName();
String reply = "File confirmed" + fileName;
return Response.status(Status.OK).entity(reply).build();
}
}
的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>org.sime.seven</groupId>
<artifactId>managefiles</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>managefiles</name>
<build>
<finalName>managefiles</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<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-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
答案 0 :(得分:5)
我发现,正如评论中所提到的,要使用泽西岛的FormDataMultipart
类,需要注册MultipartFeature
所以我将init param添加到web.xml中,如下所示
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
它运作得很好。