users@jersey.java.net

Re: [Jersey] how to received indexed parameter using _at_FormParam

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 24 Mar 2010 10:10:31 +0100

Hi,

It is hard to know what is going wrong without a complete testable
example, or more infor on what is going wrong e.g. errors output from
the servlet or the response returned to the client.

See attached for a sample that will run in GF v3 an exercises
uploading. Check the servlet logs for output (server side request
logging is enabled).

The "[]" characters in the form input name have no semantics w.r.t to
multiple values. JAX-RS/Jersey considers the name an opaque string. So
either of the following should work:


     @POST
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     public Address setAddress(@FormParam("address[]") String[]
address ) {
     ....
     }

     @POST
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     public Address setAddress(@FormParam("address[]") List<String>
address ) {
     ....
     }

Paul.







On Mar 24, 2010, at 6:17 AM, Rahul Joshi wrote:

> Hi All,
>
> I am trying to parse indexed request parameter sent in post request.
> This is the html which I am using to make the request:
> <form action="rest/resource method="POST">
> <label >Address 1</label><input name="address[]" />
> <br/>
> <label >Address 2</label><input name="address[]" />
> <br/>
> <label >Address 3</label><input name="address[]" />
> <br/>
>
> <input type="submit" value="Submit" />
> </form>
>
> at the server side here are different combination that I tried (none
> of this works):
> 1.
> @POST
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
> public Address setAddress(@FormParam("address") String[]
> address ) {
> ....
> }
>
> 2.
> @POST
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
> public Address setAddress(@FormParam("address[]") String[]
> address ) {
> ....
> }
>
> 3.
> @POST
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
> public Address setAddress(@FormParam("address") List<String>
> address ) {
> ....
> }
>
> 4.
> @POST
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
> public Address setAddress(@FormParam("address[]") List<String>
> address ) {
> ....
> }
>
> From FormParam's documentation:
> "The type T of the annotated parameter must either:
> • Be a primitive type
> • Have a constructor that accepts a single String argument
> • Have a static method named valueOf that accepts a single String
> argument (see, for example, Integer.valueOf(String))
> • Be List<T>, Set<T> or SortedSet<T>, where T satisfies 2 or 3
> above. The resulting collection is read-only."
> Using List<String> satisfies the condition 4 mentioned above but it
> doesn't work.
>
>
> Can anybody suggest how to retrieve the indexed parameter without
> using MultivaluedMap?
>
> Thanks
> -Rahul