jersey RESTful api 使用 FormDataMultipart 时回复 415 不支持的媒体类型
jersey RESTful api replies 415 unsupported media type when using FormDataMultipart
我正在使用 jersey 2.22.1 在 java 中构建 RESTful API。我有一个资源 class,我打算在其中接收客户将通过表单上传的文件。我的问题是我一直从服务器收到 415 unsupported media type 响应。有趣的是,客户端(Postman chrome 扩展名)收到状态 0,当文件为 jpg image
时连接到服务器时出错
资源 class 中还有另一种用于测试目的的方法,效果很好。我用谷歌搜索了 Whosebug 网站,可以找到类似的案例,但对我的案例没有帮助。感谢所有帮助,提前致谢。
资源class
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>
我发现,如评论中所述,要使用球衣中的 FormDataMultipart
class,需要注册 MultipartFeature
所以我将初始化参数添加到 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>
而且效果很好。
我正在使用 jersey 2.22.1 在 java 中构建 RESTful API。我有一个资源 class,我打算在其中接收客户将通过表单上传的文件。我的问题是我一直从服务器收到 415 unsupported media type 响应。有趣的是,客户端(Postman chrome 扩展名)收到状态 0,当文件为 jpg image
时连接到服务器时出错资源 class 中还有另一种用于测试目的的方法,效果很好。我用谷歌搜索了 Whosebug 网站,可以找到类似的案例,但对我的案例没有帮助。感谢所有帮助,提前致谢。
资源class
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>
我发现,如评论中所述,要使用球衣中的 FormDataMultipart
class,需要注册 MultipartFeature
所以我将初始化参数添加到 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>
而且效果很好。