users@jersey.java.net

Re: [Jersey] 1)regaring testing http POST of restful webservices with multiple parameters and 2) restful file uploading

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 14 May 2008 12:45:53 +0200

Marc Hadley wrote:
> On May 11, 2008, at 2:04 AM, Jyothsna L wrote:
>>
>> 1) while testing the POST of restful web services, I always get a
>> string content as parameter that needs to be filled while posting.
>> so , I am parsing the string based some delimiters like comma and then
>> saving each element in database.
>> for example my database has email,name as its columns in one table.
>> so if my POST parameter : content has jyothsna_at_yahoo.com, jyothsna. I
>> am parsing it into two elements jyothsna_at_yahoo.com(email) and
>> jyothsna(name).
>> and then I am creating entries in the table.
>>
>> I tried to add two parameters to postxml() instead of one. but, its
>> not working properly.
>>
>> so do you suggesting parsing one single parameter or is there any
>> other good way to have multiple parameters.
>>
> The body of a post is mapped to a single parameter but you can use a
> custom type for that parameter. If you do that you need to create a
> MessageBodyReader<CustomType> which Jersey will use to convert the
> posted data into an instance of the custom type.
>

An alternative is to use the "application/x-www-form-urlencoded" media
type with the Java type MultivaluedMap<String, String>, then you could POST:

   email=yothsna_at_yahoo.com&name=jyothsna

for example:

   @POST
   @ConsumeMime("application/x-www-form-urlencoded")
   public void post(MultiValuedMap<String, String> form) {
      String email = form.getFirst("email");
      String name = form.getFirst("name");
   }

We have previously discussed on the list about having "form beans", for
example:

   @FormBean
   public class Bean {
      public String email;
      public String name;
   }

   @POST
   @ConsumeMime("application/x-www-form-urlencoded")
   public void post(Bean b) {
     ...
   }

We understand the technical details to do this (it is a
MessageBodyReader that operates on @FormBean annotated classes) it is
just getting the time to do it!

Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109