You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by "Bhojraj, Santosh" <Sa...@ustrust.com> on 2007/08/16 20:36:07 UTC
Axis 2 ver 1.2: Returning Maps from Web service
Hi:
I am working on the POJO web service in the Axis2 (version 1.2)
samples and I added a method in there to return a Map of String to
'Entry' objects.
The new service is below:
AddressBookService.java
public Map<String, Entry[]> findEntriesByState()
{
Map<String, Entry[]> stateToEntriesMap = new HashMap<String, Entry[]>(
entries.size() );
for(Iterator iter = entries.keySet().iterator(); iter.hasNext();)
{
Entry value = (Entry) entries.get( iter.next() );
if ( stateToEntriesMap.containsKey( value.getState() ) )
{
Entry[] entryArray = (Entry[]) stateToEntriesMap.get(
value.getState() );
Entry[] newEntryArray = new Entry[ entryArray.length + 1] ;
System.arraycopy( entryArray, 0, newEntryArray, 0, entryArray.length
);
newEntryArray[ newEntryArray.length - 1 ] = value;
stateToEntriesMap.put( value.getState(), newEntryArray );
} else
{
stateToEntriesMap.put( value.getState(), new Entry[] { value } );
}
}
System.out.println("Num Entries in STATE - 2 - ENTRY MAp?" +
stateToEntriesMap.size() );
return stateToEntriesMap;
}
I then changed the sample client provided, AddressBookRPCClient.java,
to call this service and display the
results. The new code added is as below:
AddressBookRPCClient.java
/*
Find STATE - 2 - ENTRIES Map
*/
System.out.println("\n ** Executing Find Entries By State *** \n " );
QName opFindEntriesByState = new QName("
http://service.addressbook.sample/xsd", "findEntriesByState");
Object[] opFindEntriesByStateArgs = new Object[0] ;
Map<String, Entry[]> stateToEntriesProto = new HashMap<String,
Entry[]>(0);
// Class[] entriesByStatereturnTypesList = new Class[] {
HashMap.class };
Class[] entriesByStatereturnTypesList = new Class[] {
stateToEntriesProto.getClass() };
Object[] entriesByStateResponse =
serviceClient.invokeBlocking(opFindEntriesByState,
opFindEntriesByStateArgs,
entriesByStatereturnTypesList);
System.out.println("Find Entries By State: Num RESPONSE Entries = " +
(null != entriesByStateResponse ? entriesByStateResponse.length : 0
) );
System.out.println("Find Entries By State: RESPONSE Entry class = " +
entriesByStateResponse[0].getClass() );
Map entries2StateMap = (HashMap) entriesByStateResponse[0];
System.out.println("Num Entries in STATE - TO - ENTRIES Map:" +
entries2StateMap.size() );
When I run this using 'ant rpc.client', the execution is successful
but the returned map is empty i.e. I see this in the output.
[java] ** Executing Find Entries By State ***
[java]
[java] Find Entries By State: Num RESPONSE Entries = 1
[java] Find Entries By State: RESPONSE Entry class = class
java.util.HashMa
p
[java] Num Entries in STATE - TO - ENTRIES Map:0
As cen be seen from the last line, the returned Map is empty even
though there service returns a non-empty Map. This can be verified by
the system
out on the server :
2007-08-16 13:29:31,504 INFO [STDOUT] Num Entries in STATE - 2 - ENTRY
MAp? 4
Any ideas how this can resolved ? Should serializers / deserializers
be specified for the Map object ?
I have attached the WSDL for reference.
Other info:
JRE ver: 1.5
App Server: JBoss 4.0.3
Thanx !!
Santosh
Re: Axis 2 ver 1.2: Returning Maps from Web service
Posted by Amila Suriarachchi <am...@gmail.com>.
hi,
Axis2 rmi which added recently ( So you can find it only in nighly builds)
supports HashMaps. if this is a feasible solution for you please try that.
Amila.
On 8/17/07, Bhojraj, Santosh <Sa...@ustrust.com> wrote:
>
> Hi:
> I am working on the POJO web service in the Axis2 (version 1.2) samples
> and I added a method in there to return a Map of String to 'Entry' objects.
> The new service is below:
>
> *AddressBookService.java*
>
> public Map<String, Entry[]> findEntriesByState()
> {
> Map<String, Entry[]> stateToEntriesMap = new HashMap<String, Entry[]>(
> entries.size() );
> for(Iterator iter = entries.keySet().iterator(); iter.hasNext();)
> {
> Entry value = (Entry) entries.get( iter.next() );
> if ( stateToEntriesMap.containsKey( value.getState() ) )
> {
> Entry[] entryArray = (Entry[]) stateToEntriesMap.get( value.getState()
> );
> Entry[] newEntryArray = new Entry[ entryArray.length + 1] ;
> System.arraycopy( entryArray, 0, newEntryArray, 0, entryArray.length);
> newEntryArray[ newEntryArray.length - 1 ] = value;
>
> stateToEntriesMap.put( value.getState(), newEntryArray );
> } else
> {
> stateToEntriesMap.put( value.getState(), new Entry[] { value } );
> }
> }
> System.out.println("Num Entries in STATE - 2 - ENTRY MAp?" +
> stateToEntriesMap.size() );
> return stateToEntriesMap;
> }
>
> I then changed the sample client provided, AddressBookRPCClient.java, to
> call this service and display the
> results. The new code added is as below:
>
> *AddressBookRPCClient.java*
> **
> /*
> Find STATE - 2 - ENTRIES Map
> */
> System.out.println("\n ** Executing Find Entries By State *** \n " );
> QName opFindEntriesByState = new QName("
> http://service.addressbook.sample/xsd", "findEntriesByState");
>
>
> Object[] opFindEntriesByStateArgs = new Object[0] ;
> Map<String, Entry[]> stateToEntriesProto = new HashMap<String,
> Entry[]>(0);
> // Class[] entriesByStatereturnTypesList = new Class[] {
> HashMap.class };
> Class[] entriesByStatereturnTypesList = new Class[] {
> stateToEntriesProto.getClass() };
>
> Object[] entriesByStateResponse = serviceClient.invokeBlocking
> (opFindEntriesByState,
> opFindEntriesByStateArgs,
> entriesByStatereturnTypesList);
>
> System.out.println("Find Entries By State: Num RESPONSE Entries = " +
> (null != entriesByStateResponse ? entriesByStateResponse.length : 0 )
> );
> System.out.println("Find Entries By State: RESPONSE Entry class = " +
> entriesByStateResponse[0].getClass() );
>
> Map entries2StateMap = (HashMap) entriesByStateResponse[0];
> System.out.println("Num Entries in STATE - TO - ENTRIES Map:" +
> entries2StateMap.size() );
>
> When I run this using 'ant rpc.client', the execution is successful
> but the returned map is empty i.e. I see this in the output.
>
> [java] ** Executing Find Entries By State ***
> [java]
> [java] Find Entries By State: Num RESPONSE Entries = 1
> [java] Find Entries By State: RESPONSE Entry class = class
> java.util.HashMa
> p
> [java] *Num Entries in STATE - TO - ENTRIES Map:0*
>
> As cen be seen from the last line, the returned Map is empty even
> though there service returns a non-empty Map. This can be verified by the
> system
> out on the server :
>
> 2007-08-16 13:29:31,504 INFO [STDOUT] Num Entries in STATE - 2 - ENTRY
> MAp? *4*
> **
> * *Any ideas how this can resolved ? Should serializers / deserializers
> be specified for the Map object ?
>
> I have attached the WSDL for reference.
>
> *Other info:*
> JRE ver: 1.5
> App Server: JBoss 4.0.3
>
> Thanx !!
> Santosh
>
> * *
>
>
>
>
> **
>
>
>
>
>
>
>
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
> For additional commands, e-mail: axis-user-help@ws.apache.org
>
>
--
Amila Suriarachchi,
WSO2 Inc.