具有CXF的JAX-RS/请放心:处理多参数文件上传 [英] JAX-RS with CXF / rest-assured: Handling multiparam file upload
问题描述
我想上传JPG文件和JSON序列化的Java对象.在服务器上,我使用的是Apache CXF,在客户端上,我的集成测试是 rest-assured .
I want to upload a JPG file and a JSON-serialized Java object. On the server I am using Apache CXF, on the client I am integration testing with rest-assured.
我的服务器代码如下:
@POST
@Path("/document")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response storeTravelDocument(
@Context UriInfo uriInfo,
@Multipart(value = "document") JsonBean bean,
@Multipart(value = "image") InputStream pictureStream)
throws IOException
{}
我的客户代码如下:
given().
multiPart("document", new File("./data/json.txt"), "application/json").
multiPart("image", new File("./data/image.txt"), "image/jpeg").
expect().
statusCode(Response.Status.CREATED.getStatusCode()).
when().
post("/document");
当我从文件中读取json部分时,一切正常,如第一多部分行中所示.但是,当我想序列化json实例时,我遇到了问题.我尝试了许多变体,但没有一个起作用.
Everything works fine when I read the json part from the file as in the first multiPart line. However, when I want to serialize the json instance I come into problems. I tried many variants, but none worked.
我认为该变体应该可以工作:在客户端上
I thought this variant should work: on the client
JsonBean json = new JsonBean();
json.setVal1("Value 1");
json.setVal2("Value 2");
given().
contentType("application/json").
formParam("document", json).
multiPart("image", new File("./data/image.txt"), "image/jpeg").
...
并在服务器上
public Response storeTravelDocument(
@Context UriInfo uriInfo,
@FormParam(value = "document") JsonBean bean,
@Multipart(value = "image") InputStream pictureStream)
但没有.谁能告诉我应该怎么办?
but no. Can anyone tell me how it should be?
推荐答案
Multipart/form-data遵循多部分MIME数据流的规则,请参见问题166 .
Multipart/form-data follows the rules of multipart MIME data streams, see w3.org. This means that each part of the request forms a part in the stream. Rest-assured supports already simple fields (strings), files and streams, but not object serialization into a part. After asking on the mailing list, Johan Haleby (the author of rest-assured) suggested to add an issue. The issue is already accepted, see issue 166.
服务器将保持不变:
@POST
@Path("/document")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response storeTravelDocument(
@Context UriInfo uriInfo,
@Multipart(value = "document") JsonBean bean,
@Multipart(value = "image") InputStream pictureStream)
throws IOException
{}
客户端代码如下:
given().
multiPartObject("document", objectToSerialize, "application/json").
multiPart("image", new File("./data/image.txt"), "image/jpeg").
expect().
statusCode(Response.Status.CREATED.getStatusCode()).
when().
post("/document");
也许名称"multiPartObject"将更改.一旦实施,我们就会看到.
Maybe the name "multiPartObject" will change. We will see once it is implemented.
这篇关于具有CXF的JAX-RS/请放心:处理多参数文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!