Jersey Grizzly Rest 如何提取 "form-data" 参数
Jersey Grizzly Rest How to extract "form-data" parameters
考虑以下函数:
@POST
@Path("/handle_response")
@Produces(MediaType.APPLICATION_JSON)
public ResponseJsonBean handle(@FormParam("first") Integer first, @FormParam("second") Integer second)
{
////// bla bla
}
当我发出 POST x-www-form-urlencoded 请求时调用上述函数。但是当我发出 POST 表单数据请求时,该函数没有被调用。为什么在后一种情况下不调用它?以及如何制作适用于后一个请求的功能。
是的 application/x-www-form-urlencoded
和 multipart/form-data
是完全不同的类型和格式。通过发送的类型发现读取请求的正确提供者。
对于多部分支持,您应该添加 Jersey 多部分依赖项
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey2.version}</version>
</dependency>
然后在你的资源方法中做类似
的事情
@POST
@Path("/upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadFile(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition cdh) throws Exception{
}
另请参阅:
- An example with the client side also
- Jersey Multipart Documentation
考虑以下函数:
@POST
@Path("/handle_response")
@Produces(MediaType.APPLICATION_JSON)
public ResponseJsonBean handle(@FormParam("first") Integer first, @FormParam("second") Integer second)
{
////// bla bla
}
当我发出 POST x-www-form-urlencoded 请求时调用上述函数。但是当我发出 POST 表单数据请求时,该函数没有被调用。为什么在后一种情况下不调用它?以及如何制作适用于后一个请求的功能。
是的 application/x-www-form-urlencoded
和 multipart/form-data
是完全不同的类型和格式。通过发送的类型发现读取请求的正确提供者。
对于多部分支持,您应该添加 Jersey 多部分依赖项
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey2.version}</version>
</dependency>
然后在你的资源方法中做类似
的事情@POST
@Path("/upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadFile(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition cdh) throws Exception{
}
另请参阅:
- An example with the client side also
- Jersey Multipart Documentation