Reflection Magic for to the WSF Project
I just added basic support for multi-value parameters to the Polar Rose Web Service Foundation project. This was mostly done using some Java reflection and generics magic.
An example will explain it all. Here is a simple web service action implementation that takes a list of integers as a parameter.
public class AddNumbersAction implements WebServiceActionHandler
{
private List<Integer> numbers;
@WebServiceParameter
public void setNumbers(List<Integer> numbers) {
this.numbers = numbers;
}
public Object execute(WebServiceActionContext context) throws ActionHandlerException {
int total = 0;
for (Integer number : numbers) {
total += number;
}
return total;
}
}
The framework takes care of constructing the list. It will also do the right conversion of request parameter values to the list’s component type when adding the items. Reflection magic.
This action can be called with:
http://localhost:8080/api
?Action=AddNumbers
&Numbers.0=1
&Numbers.1=2
&Numbers.2=3
&Numbers.3=4
&Numbers.4=5
.. standard parameters omitted for brevity ...
And produces the following JSON reply:
{ 'webServiceCallId': 'f9a57f56-905d-43c4-86da-d9ef4418d54b', 'response': 15}
So, this makes it really easy to supply lists of simple values to web service actions.
Unfortunately the speed dropped about 20% but I will make up for that in a later release when i cleanup and optimize this code. The reflection APIs are pretty cool but can also cause a performance hit. Caching lookups will probably bring the speed back. But 800 requests/second on a MacBook that is also playing some iTunes is still pretty good I guess
In a future release I will make this work with POJOs so that you can also do things like City.0.Name=Amsterdam&City.0.CountryCode=NL. I’m not really needing that in the web services that I’m working on now but it is certainly on the todo list.
You can find two example actions, AddNumbers and ConcatStrings, in the WSF Examples project. Also hosted at Google Code. Enjoy!
Modified
