Solved

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

Posted on 2010-08-24
12
3,391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 60

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 60

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

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 60

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 110

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 60

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 60

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

717 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