Solved

how to handle an array of parameters in a PUT REST web service request

Posted on 2010-08-24
12
3,175 Views
Last Modified: 2013-11-23
Hello,

I'm designing a REST web service PUT method that has an Array of query string parameters.  How is that array represented in the URL and how would I map those params to a Collection on the Java side?  I'm using RESTEasy as my jax-rs implementation.
0
Comment
Question by:cgray1223
  • 6
  • 5
12 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33520994
With query string, IIRC, you would simply:

@QueryParam("name") String[] names

However, this may also be helpful:
http://www.redhat.com/docs/en-US/JBoss_Enterprise_Application_Platform/5.0.0/html-single/RESTEasy/index.html#JAXB_Collections
0
 

Author Comment

by:cgray1223
ID: 33523660
mwvisa1, thanks!  How would the url look?  I'm unsure how to construct it to call the service with an array of querystring params...
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33523963
The URL should look like:

?name=John&name=Joe&name=Jeff

Just same parameter repeated; however, I just pulled up to do a test in Eclipse and it is acting up, so just verifying...try on your end and see as might just be my setup being just in Eclipse and not actual web server.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33526101
Let me know how your tests go please since I can't seem to get my development environment to simulate it at the moment and can't find good documentation on it for you. The above does should you though how to take it in via JAXB collections.
0
 

Author Comment

by:cgray1223
ID: 33527164
I don't think you can pull from the query string to populate an array using the JAX-RS spec but I could be mistaken.  The RESTEasy docs say to do the below:

@PUT
 @Path("/path/{id}/message")
 @Consumes({"application/xml","application/json"})
 public void storeMessage(Message[] message){
  message[0].getValue();
  message[1].getValue
}

The below xml would have to go in the request entity body and then a put request is sent, I guess...
<collection>  
      <message>
         <unixTime>123223333</unixTime>
         ....
       </message>    
 <collection>

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33527354
Exactly, that is the technique from link in http:#a33520994 I was referring to. It is definitely a way, but could have sworn that this would work in query param also, but since I can't find documentation my only suggestion would be to test. I asked Java ZA to take a look so may have another solution or confirmation that XML is on the only way to go unless you want to parse out string yourself (i.e., send one parameter and use split() or something to that effect to bring into array).
0
 

Author Comment

by:cgray1223
ID: 33527994
ok, thanks for your help, please keep me updated.  I'm just looking for the best way possible.  
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33553099
Here is how I might set up the array in the GET string.  There is a PHP code example to process this, below.  Not Java, but hopefully the comments will make it somewhat clear.  

http://www.laprbass.com/RAY_REST_get_last_name_array.php?key=ABC&name[]=Ray&name[]=Fred&name[]=Richard

HTH, ~Ray
<?php // RAY_REST_get_last_name_array.php
error_reporting(E_ALL);



// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&name[]=Ray&name[]=Brian');



// OUR DATA MODEL CONTAINS ALL THE ANSWERS
$dataModel
= array
( 'Brian'   => 'Portlock'
, 'Ray'     => 'Paseur'
, 'Richard' => 'Quadling'
)
;


// TEST THE API KEY - THE ASSUMPTION IS FAILURE
$key = FALSE;

// IF THE URL CONTAINS THE KEY
if (isset($_GET["key"])) $key = $_GET["key"];

// IF THE KEY FAILS TO MATCH OUR OUR REQUIRED 'ABC'
if ($key !== 'ABC') die('BOGUS API KEY');



// NORMALIZE THE 'name=' ARGUMENT INTO AN ARRAY
$names = array();
if (isset($_GET["name"]))
{
    if (!is_array($_GET["name"]))
    {
        // NOT AN ARRAY - MAKE IT AN ARRAY
        $names[] = $_GET["name"];
    }
    else
    {
        // ALREADY AN ARRAY - COPY IT
        $names = $_GET["name"];
    }
}
else
{
    die('NO NAME');
}

// LOOK UP THE FIRST NAMES AND FIND THE LAST NAMES
$out = '';
foreach ($names as $first_name)
{
    // IF THE NAME IS FOUND
    if (array_key_exists($first_name, $dataModel))
    {
        $out .= $first_name . ' ' . $dataModel[$first_name] . PHP_EOL;
    }
    // IF THE NAME IS NOT FOUND
    else
    {
        $out .= $first_name . ' UNKNOWN' . PHP_EOL;
    }
}

// RETURNS THE DATA STRING
die($out);

Open in new window

0
 

Author Comment

by:cgray1223
ID: 33564271
With RESTEasy there is a very nice annotation called @Wrapped and it will map JSON or XML requests to a JAXB Entity.  In addition to mapping to an Array, any Collection can be used (List, Set..etc).  The outer "customers" xml wrapper must match the Wrapped's name property and thats it.  

XML Request:
<?xml version="1.0" encoding="UTF-8"?>
<customers>
 <customer>
   <name>bill</name>
 </customer>
 <customer>
   <name>monica</name>
 </customer>
 </customers>

RESTEasy REST Web Service Resource:
@PUT
@Path("/user/store")
@Consumes("application/xml")
public void putCustomers(@Wrapped(name="customers") Customer[] customers)
{
    System.out.println(ccustomers.get(0).getName());
}
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33565237
Glad you figured that out.  Note that it is what I linked you too here - http:#a33520994 - in case you need further reference.  I was merely hoping to find you another alternative since your question said through query string parameters.  JAXB collection, definitely a good option!

Anyway, best regards and happy coding,

Kevin
0
 

Author Comment

by:cgray1223
ID: 33573425
thanks, the document you referred me to was a good source, but it was missing @Wrapped.  Without that you wont be able to put/post xml to the REST resource.  Thanks for the help!
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33574996
Yes they showed without the annotation for whatever reason.  Noticed they did the same with the QueryParam example.  As I said, I am glad you found that as I realized my conversations about Wrappedd were with Java ZA who didn't find example of working around this to conirm another way so I would just go with what we know works...
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GUI builder for Eclipse? 8 37
servlet doXXX methods 3 51
mysql jsp example issue 32 38
Is Applet the way to go for my drag and drop system? 8 17
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question