Igor's Blog
Programming, DIY, Games, Hacks, and Tech

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

comments powered by Disqus
Other posts you may like...

Recent Blog Posts

A hack to create an uber jar with the Maven Shade Plugin using local jar files

Is it worth it? Apple USB-C Digital Multi AV adapter vs a cheap eBay clone

Running X11 graphical applications after changing to another user using 'sudo su'

How to stop Facebook using ad images as post sharing thumbnails

Picking lists for Atari Lynx capacitor replacement kits

Performance - 2012 Retina MacBook Pro vs 2017 MacBook Pro

Look and feel - 2012 Retina MacBook Pro vs 2017 MacBook Pro

Using math to work out the diameter of hard to reach water cooling pipes

How to get the unreachable shard in the Hinterlands in DragonAge Inquisition

Measy RC12 wireless 2.4Ghz keyboard touchpad review

Recent Galleries

Monument Valley 2 is released and does not disappoint

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

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

Blog Activity

Blog Activity
Don't forget to
me for more great articles!
Don't show this again