You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sergey Beryozkin <se...@iona.com> on 2009/11/08 20:00:46 UTC

Re: JAX-RS form reading, I'm stumped

Hi Benson

I've enhanced a MultipartProvider to delegate to other providers in case of
multipart/form-data.
For example a request like [1]

can be handled by a method with the following signature :

@POST
@Path("/books/jsonform")
@Consumes("multipart/form-data")
public Response addBookJsonFromForm(Book b1)  {...}

Similarly. a request like [2]

can be handled by a method with the following signature :

@POST
@Path("/books/jsonjaxbform")
@Consumes("multipart/form-data")
public Response addBookJaxbJsonForm(@Multipart("jsonPart") Book b1, 
                                        @Multipart("bookXML") Book b2) {}

Note that once you have more than two parts you need to start using
@Mutipart, the values can refer  to either ContentId header or to
ContentDisposition/name.

Note that at the moment using @Multipart is preferred to using @FormParam
unless you have a plain name/value submission. The reason is that @Multipart
can also specify an expected media type of the individual part.

When dealing with multiple parts one can avoid using  @Multipart and just
use List : List<Atachment>, List<Book>, etc

Finally, I've added the support for handling multipart/form-data with
multiple files, For ex, a request like 

http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/attachmentFormJsonFiles

can be handled by a method with the signature like :

@POST
    @Path("/books/filesform")
    @Produces("text/xml")
    @Consumes("multipart/form-data")
    public Response addBookFilesForm(@Multipart("owner") String name, 
                                     @Multipart("files") List<Book> books)
{}

I'll work on updating the docs later on. A minor enhancement will be applied
to the client api later on to let users submit multiple files with
multipart/form-data

Sergey  


 
 


[1]
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/attachmentFormJson
[2]
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/resources/attachmentFormJsonJaxb


Sergey Beryozkin wrote:
> 
> Hi Benson
> 
> AFAIK multipart/form-data is mainly used to submit files from the forms
> [1], though individual parts may contain
> "application/x-www-form-urlencoded" data, this is my interpretation at
> least and this is how I implemented the way multipart/form-data is
> handled. So it should really be :
>     @Consumes("multipart/form-data")
>     public Response addGazetteer(@FormParam("gazetter")  String gazetteer)
> {
>     }
>   
> in other words CXF FormProvider will not delegate to other providers
> (JAXB/JSON, etc) when dealing with "multipart/form-data"; perhaps it
> should ?
> 
> It is MultipartProvider which deals with multipart/related,
> multipart/mixed, etc, which will delegate. It is this provider which will
> notice @Multipart(type = "application/json").
> 
> There's a couple of options. If you can actually afford specifying say
> multipart/mixed then it will work as expected. Another option is to
> register a simple RequestFilter impl which will change multipart/form-data
> to say multipart/mixed. Finally you can do 
> 
> @Consumes("multipart/form-data")
>     public Response addGazetteer(@FormParam("gazetter")  Gazetter
> gazetteer) {
>     }
>   
> and register a ParameterHandler<Gazetter> which will use a JSONProvider to
> convert a JSON sequence into Gazetter
> 
> cheers, Sergey
> 
> [1]http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
> 
> 
> bimargulies wrote:
>> 
>> Oct 10, 2009 5:08:59 PM org.apache.cxf.jaxrs.utils.JAXRSUtils
>> readFromMessageBody
>> WARNING: .No message body reader found for request class : Gazetteer,
>> ContentType :
>> multipart/form-data;boundary=hxz8idwzzxgwwr7p0v1vregmn2wxnajqg0f_bxk6.
>> Oct 10, 2009 5:08:59 PM
>> org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
>> WARNING: WebApplicationException has been caught : no cause is available
>> 
>> And my function is:
>> 
>>     @POST
>>     @Path("/gazetteer/new")
>>     @Consumes("multipart/form-data")
>>     public Response addGazetteer(@Multipart(type = "application/json")
>> Gazetteer gazetteer) {
>>         try {
>>             configDb.addGazetteer(gazetteer);
>>         } catch (Exception e) {
>>             return Response.status(500).build();
>>         }
>>         return Response.ok().build();
>>     }
>> 
>> The client sends a multipart form posting with JSON in the one and only
>> part, and I expect it to get mapped. What am I missing?
>> 
>> 
> 
> 

-- 
View this message in context: http://old.nabble.com/JAX-RS-form-reading%2C-I%27m-stumped-tp25838462p26256760.html
Sent from the cxf-user mailing list archive at Nabble.com.