Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming a multipart/form-data via RESTful CXF

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?

like image 696
fcm Avatar asked Mar 07 '13 12:03

fcm


People also ask

What is Apache CXF used for?

Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

What is multipart REST API?

Multipart/Form-Data is a popular format for REST APIs, since it can represent each key-value pair as a “part” with its own content type and disposition. Each part is separated by a specific boundary string, and we don't explicitly need Percent Encoding for their values.

How does multi part form data work?

Multipart/form-data is one of the most used enctype/content type. In multipart, each of the field to be sent has its content type, file name and data separated by boundary from other field. No encoding of the data is necessary, because of the unique boundary. The binary data is sent as it is.


1 Answers

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.

like image 101
Chris Avatar answered Oct 08 '22 06:10

Chris