Consuming a multipart/form-data via RESTful CXF

白昼怎懂夜的黑 提交于 2020-01-11 02:03:29

问题


I've been working in a webservice that consumes and produces JSON files using Apache CXF in conjuction with Jackson.
However, one of the service's methods should be able to save an uploaded image from a mobile application that makes a multipart/form-data POST request to my webservice, and I don't know how to treat this kind of content-type within my context. We usually create "Request" and "Response" objects to consume and produce the JSON, however, I'm afraid this would not work for this case.

This is the Request format:

Content-type: multipart/form-data
"Description": text/plain
"Path": text/plain
"Image": image/jpeg

How to correctly consume this kind of request and save the image server-side?


[EDIT]

I managed to consume multipart/form-data by using this:

public returnType savePicture(
                @Multipart(value = "mode", type = "text/plain") String mode,
                @Multipart(value = "type", type = "text/plain") String type,
                @Multipart(value = "path", type = "text/plain") String path
                @Multipart(value = "image", type = "image/jpeg") Attachment image
            ) 
    {

However, when trying to consume the following POST request:

Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="mode"

T
--AaB03x
content-disposition: form-data; name="type"

M
--AaB03x
content-disposition: form-data; name="path"

c:/img/
--AaB03x
content-disposition: form-data; name="image"; filename="image.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

imgdata
--AaB03x--

I'm getting the following error:

javax.ws.rs.BadRequestException: org.apache.cxf.jaxrs.utils.multipart.MultipartReadException: No multipart with content id type found, request content type : multipart/form-data;boundary=AaB03x

When I consume only mode, for instance, it works fine. It only breaks for 2 or more parameters. Any idea for why is that wrong?


回答1:


I faced similar issue sometime back.

The following code did the trick for me

@POST
@Consumes("multipart/form-data")
public void yourMethod(<params>) throws Exception {
}

In short, it is I think the @Consumes annotation you are missing.




回答2:


It seems we found the problem, and it was related to the format of the request. The correct format should have been:

Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="mode"

T--AaB03x

content-disposition: form-data; name="type"

M--AaB03x

content-disposition: form-data; name="path"

c:/img/--AaB03x

content-disposition: form-data; name="image"; filename="image.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

imgdata--AaB03x--

Changing to this format allowed me to consume the other parameters.




回答3:


For consuming multipart form data. use @consumes tag & provide "multipart/form-data" along with value parameter like

@Consumes(value = "multipart/form-data")

refer https://jnorthr.wordpress.com/2012/07/10/http-header-content-type-and-encodings/



来源:https://stackoverflow.com/questions/15271573/consuming-a-multipart-form-data-via-restful-cxf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!