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

WebLogic Maven Plugin - How to fix the MojoExecutionException: The artifact location was not specified

jPhotoFrame version 0.4 released with a whole new layout engine

Upcycling a couple of old broken lamps to create something amazing

A custom exception mapper and writer for a RESTful JAX-RS Jersey service

How to fix Plex error - Sorry there was a problem playing this item

Jersey JAX-RS filters and interceptors execution order for a POST request

Fix your Mac - users not showing on the macOS login screen when FileVault is enabled

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Writing a custom MessageBodyReader to process POST body data with Jersey

How to make Skype for Business responsive again on macOS

Recent Galleries

BMB-012 Nanoblock T-Rex Skeleton Model assembly

Tiny Arcade revision 6 kit assembly and decal application

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

Blogs and Friends

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

Blog Activity

Blog Activity