Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

I've written a number of articles about using Jersey over the past few months covering various aspects of RESTful services. This article is about passing in an array of values to your Resource from a form.

I'm not going to cover how to set up your application to use with Jersey as I've already covered it here.

So lets get straight into it. The Resource should be set up as follows...
 REST Service Resource
@Path("myresource")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public class MyResource {
@POST
@Path("test")
public void testArray(@FormParam("array") List<String> array) {
/* do something with the input array */
for (String s : array) {
System.out.println(s);
}
}


Note that the @Consumes is set to MediaType.APPLICATION_FORM_URLENCODED and the resource uses @POST on the method/path we're exposing to consume an array of values. The @POST is strictly not necessary but it's a better practice to use POST to submit a form rather than GET.

Now that's the Jersey side done, there's actually not much to it.

In order to make this work on the client side you need to be sure that each of your form inputs that you wish to be in the same array have the same name attribute. For example:
 HTML form
<form method="POST" action="/myresource/test">
<input name="array" type="text" />
<input name="array" type="text" />
<input name="array" type="text" />
<input name="array" type="text" />
<input name="array" type="text" />
...
</form>




When this form is submitted, the body of the request will be something like this (with some dummy values substituted):
 POST request
array=This&array=is&array=a&array=cool&array=Blog


Then if you look at the System.out output from the service, it will be something like this:
 System.out
This
is
a
cool
Blog


That's all there is to it. Just get your form method and parameter names right and it should 'just work'.

-i

Please leave your comments or feedback below!
comments powered by Disqus
Other posts you may like...
Hi! You can search my blog here ⤵
Or browse the recent top tags...

Recent Blog Posts

Coherence and weblogic.xml in different types of J2EE web app deployments

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 1 - introduction and case disassembly

jPhotoFrame updated to version 0.3.1 with an image rotation correction utility

iOS 11 pre-GM mini review before it gets revealed next week

Why you should never use Java enums as keys with Oracle Coherence caches

Hacking the VideoPak 7inch promotional brochure to work as a digital photo frame

Recent Galleries

Atari Lynx repair - Part 5 - McWill LED screen mod installation

Atari Lynx repair - Part 4 - screen cover replacement

Atari Lynx repair - Part 2 - re-capping the motherboard

Atari Lynx repair - Part 3 - broken speaker replacement

Atari Lynx repair - Part 1 - introduction and case disassembly

Building a custom Atari Lynx game box storage shelf unit in a day

Protecting old Atari Lynx game boxes with snug fit plastic sleeves

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity