Jaxrs多部分 [英] Jaxrs multipart

查看:169
本文介绍了Jaxrs多部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向媒体类型设置为multipart/form-data的jaxrs服务执行请求.该请求包含一个实体列表(xml)和一个图像列表(png,二进制).我已经按照BalusC的线程中的说明创建了请求.

I'm trying to perform a request to a jaxrs service which has media type set to multipart/form-data. This request contains a list of entities(xml) and an image(png, binary). I have created the request as described in this thread by BalusC.

在wireshark中检查请求后,该请求似乎还可以,除了ip标头校验和错误.(说些有关可能是由IP校验和卸载引起的.")

The request seems ok after inspecting it in wireshark, except for the ip header checksum being wrong.(says something about "may be caused by IP checksum offload".)

我这里最大的问题是如何在服务端处理多部分请求.我不希望包含apache.cxf,resteasy或任何种类的库.我要依靠的只是jaxrs api.

My big issue here is how to handle the multipart request on the service side. I do not wish to include any libraries from apache.cxf, resteasy or anything of the sort. All I want to rely on is the jaxrs api.

请求中的两个部分分别具有名称deliveriessignature,其中的签名是作为二进制文件发送的png图像文件.交付清单应从xml解析(实体具有xmlrootelement批注,因此,这部分单独工作).我尝试用这种方式阅读不同部分,但这确实是一个漫长的尝试;

The two parts in the request have names deliveries and signature, where the signature is a png image file sent as binary. The list of deliveries should be parsed from an xml(the entity has the xmlrootelement annotation and such, so this part works separately). I've attempted with this way of reading the different parts, but this was really a longshot;

@PUT
@Path("signOff")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void signOffDeliveries(@FormParam("deliveries") List<Delivery> deliveries, @FormParam("signature")File signature) {
    //do something with the signature(image) and the list of deliveries.
}

这当然是行不通的,如果我在Websphere上运行请求,它会给我404 http状态代码,而对嵌入式openejb(在我们的集成测试框架中)运行请求时,它会给我415.如果删除FormParam批注,则请求成功.

This does off course not work, and it gives me a 404 http status code if I run the request on Websphere, and a 415 when I run the request towards an embedded openejb (in our integration test framework). If I remove the FormParam annotations the request succeeds.

如何仅使用jaxrs api读取multipart请求的不同部分?

How can I read the different parts of the multipart request using only the jaxrs api?

编辑 好的,所以我将PUT伪装为POST,并在参数中添加了@Encoding批注,如下所示:

EDIT Ok, so I canged the PUT to POST, and added an @Encoding annotation to the params as so:

@POST
@Path("signOff")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void signOffDeliveries(
    @Encoded @FormParam("deliveries") String deliveries,
    @Encoded @FormParam("signature") File signature) {

}

现在,我将xml作为文本字符串获取,但是即使有效负载的这一部分的Content-Type设置为application/xml,我也无法将其自动解组到交货清单中.另一个问题是我收到的文件的长度为= 0,并且无法读取任何字节.

Now I get the xml as a text string, but I am not able to automatically unmarshal it to a list of deliveries even though the Content-Type of this part of the payload is set to application/xml. The other problem is that the file I receive has length==0, and I am not able to read any bytes from it.

我在这里错过了要点吗?

Am I missing an essential point here?

推荐答案

实际上,我很难理解为什么JAX-RS规范没有标准化对此的支持(我刚刚创建了

Indeed I find it hard to understand why the JAX-RS spec doesn't standardize a support for this (I've just created https://java.net/jira/browse/JAX_RS_SPEC-413 to address this).

但是,仍然可以以实现独立的方式来支持多部分表单.您可以编写自己的MultiPart形式的MessageBodyReader表单,也可以使用Apache Clerezza jaxrs.utils之类的库,该库提供一个分别与MessageBodyReader相对应的MultiPartBody对象.该库没有实现规范依赖,因此您的应用程序可以在任何jax-rs实现上运行.

However it is nevertheless possible to support multi-part forms in an implementation independent fashion. Either you write your own MessageBodyReader for MultiPart form or you use a library like Apache Clerezza jaxrs.utils which provide a MultiPartBody object which respective MessageBodyReader. This library has no implementation specification dependency so your application will run on any jax-rs implementation.

有关如何使用Clerezza jaxrs.utils的示例,请参见

For an example on how Clerezza jaxrs.utils is used see line 105 in http://svn.apache.org/viewvc/stanbol/trunk/development/archetypes/stateless-webmodule/src/main/resources/archetype-resources/src/main/java/MultiEnhancer.java?revision=1465777&view=markup. If you're not using OSGi (with white-board registration of resources) you will have to add to org.apache.clerezza.jaxrs.utils.form.MultiPartFormMessageBodyReader to your Application.

这篇关于Jaxrs多部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆