Skip to main content

How can I send an array of objects to WS

No replies
leonardo_josue
Offline
Joined: 2013-06-26
Points: 0

Hi everybody...

I'm trying to consume a WS in AXIS 1.4 sending an Array of Objects, but always get an empty array in the method than I try to execute...

I have this JavaBean Class:

<br />
package params;<br />
import java.io.Serializable;<br />
public class Person implements Serializable {<br />
  private String name;<br />
  private String lastName;<br />
  public String getName() {<br />
    return name;<br />
  }<br />
  public void setName(String name) {<br />
    this.name = name;<br />
  }<br />
  public String getLastName() {<br />
    return lastName;<br />
  }<br />
  public void setLastName(String lastName) {<br />
    this.lastName = lastName;<br />
  }<br />
  public Person(){<br />
  }<br />
}<br />

I have a Class HelloWorld with a method SayHi than recive an Array of person's, like this:

<br />
package publish;<br />
import params.Person;<br />
public class HelloWorld {<br />
  public HelloWorld(){<br />
  }<br />
  public String[] SayHi(Person[] persons){<br />
    String[] temp = new String[persons.length];<br />
    for (int i = 0; i < persons.length; i++) {<br />
      temp[i] = "Hello" + persons[i].getName() + " " + persons[i].getLastName();<br />
    }<br />
    return temp;<br />
  }<br />
}<br />

My WSDD file looks like this:

<br />
<service name="HelloWorld" provider="java:RPC"></p>
<parameter name="allowedMethods" value="*"/>
<parameter name="className" value="publish.HelloWorld"/>
    <arrayMapping qname="ns:Persons" xmlns:ns="Persons"<br />
             languageSpecificType="java:params.Person[]"<br />
             encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/><br />
</service><br />

And I invoke the WS with this code:

<br />
public static void main(String[] args) {<br />
    try {<br />
      String endpoint = "http://localhost:8084/WSTest/services/HelloWorld";<br />
      Service service = new Service();<br />
      Call call = (Call) service.createCall();<br />
      call.setTargetEndpointAddress( new java.net.URL(endpoint) );<br />
      call.setOperationName("SayHi");<br />
      String[] resultado = null;<br />
      Person[] listOfPersons = new Person[2];<br />
      Person person = new Person();<br />
      person.setName("Leonardo");<br />
      person.setLastName("Córcega");<br />
      listOfPersons[0] = person;<br />
      person = new Person();<br />
      person.setName("Josué");<br />
      person.setLastName("Ufona");<br />
      listOfPersons[1] = person;</p>
<p>      QName qname = new QName(endpoint, "Persons");<br />
      call.registerTypeMapping(Person[].class, qname, new BeanSerializerFactory(Person[].class, qname), new BeanDeserializerFactory(Person[].class, qname));<br />
      result = (String[]) call.invoke(new Object[]{listOfPersons});<br />
      for (int i = 0; i <= result.length - 1; i++) {<br />
        System.out.println("result " + String.valueOf(i) + ": ".concat(result[i]));<br />
      }<br />
    } catch (Exception e) {<br />
      System.err.println(e.toString());<br />
    }<br />
  }<br />

How I say, when I try to ran this class and consume the WS, I got an empty array in the method SayHi... I can't see where is the problem... but I hope somebody can help to me...

Best Regards
Leo.