wie ArrayList an Webservice geben?

Dieses Thema im Forum "SOA" wurde erstellt von Lenzen, 15. Feb. 2012.

  1. Lenzen

    Lenzen Neues Mitglied

    Ich versuch schon eine Ewigkeit eine ArrayList mit einem Webservice aufzurufen.
    Mein Versuch:

    Code (Java):

    import java.util.ArrayList;

    import javax.jws.WebMethod;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    /**
     * Web Service End-point implementation class
     */

       
    @WebService(name = "TEST_name", serviceName = "TEST_serviceName")
    @SOAPBinding(style=SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL)

    public class Hello {
       // Constructor
       public void Hello() {}
       
       @WebMethod
       public String sayHello(String name) {
          return "Hello, " + name + ".";
       }
       
       @WebMethod
       public int addNumbers(int number1, int number2) {
          return number1 + number2;
       }
       
       @WebMethod
       public double divideNumbers(int dividend, int divisor)
             throws Exception {
          if (divisor == 0) {
             throw new Exception("Divisor cannot be zero!");
          }
          return (double)dividend/divisor;
       }
       
       @WebMethod
        public ArrayList<Object> testArrayList() {
            ArrayList<Object> results = new ArrayList<Object>();
            int title = 1;
            String description = "Description";
            double d = 44.66;      
            long l = 66;
            String st = "Stringtest";
            results.add(title);
            results.add(description);
            results.add(d);
            results.add(l);
            results.add(st);
            return results;
        }
    }
     
    Request:
    [XML]
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://Webservice2/">
    <soapenv:Header/>
    <soapenv:Body>
    <web:testArrayList/>
    </soapenv:Body>
    </soapenv:Envelope>
    [/XML]

    Response
    [XML]
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
    <ns2:testArrayListResponse xmlns:ns2="http://Webservice2/">
    <return/>
    </ns2:testArrayListResponse>
    </S:Body>
    </S:Envelope>
    [/XML]

    Nun habe ich folgende Zeile auskommentiert:
    Code (Java):

    @SOAPBinding(style=SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL)
     
    Als Response bekomme ich (fast das was ich will):
    [XML]
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
    <ns2:testArrayListResponse xmlns:ns2="http://Webservice2/">
    <return xsi:type="xs:int" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">1</return>
    <return xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">Description</return>
    <return xsi:type="xs:double" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">44.66</return>
    <return xsi:type="xs:long" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">66</return>
    <return xsi:type="xs:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">Stringtest</return>
    </ns2:testArrayListResponse>
    </S:Body>
    </S:Envelope>
    [/XML]

    und einen Validerungsfehler:
    Invalid xsi:type qname: 'xs:int'
    Invalid xsi:type qname: 'xs:string'
    Invalid xsi:type qname: 'xs:double'
    Invalid xsi:type qname: 'xs:long'
    Invalid xsi:type qname: 'xs:string'


    Was mache ich falsch?
     
  2. Lenzen

    Lenzen Neues Mitglied

    Mittlerweile denke ich, dass man komplexe Datentypen nicht an einen Webservice geben kann. Über eine Bean klappt das. Vielleicht hat ja noch jemand eine Idee?

    Meine komplette Lösung bisher:
    Code (Java):

    package test;

    import java.util.ArrayList;
    import java.util.Collection;

    import javax.jws.WebMethod;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import javax.jws.soap.SOAPBinding;
    /**
     * Web Service End-point implementation class
     */

       
    @WebService(name = "TEST_name", serviceName = "TEST_serviceName")
    //@SOAPBinding(style=SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL)
     
    public class Hello {
       // Constructor
       public void Hello() {}
       
       @WebMethod
       public String sayHello(String name) {
          return "Hello, " + name + ".";
       }
       
       @WebMethod
       public int addNumbers(int number1, int number2) {
          return number1 + number2;
       }
       
       @WebMethod
       public double divideNumbers(int dividend, int divisor)
             throws Exception {
          if (divisor == 0) {
             throw new Exception("Divisor cannot be zero!");
          }
          return (double)dividend/divisor;
       }
       
       @WebMethod
       public ArrayList<Param> testArrayList(ArrayList<Param> results) {    
           /**     
           Param p = new Param();      
           int title = 222;
           String description = "Description2";
           double d = 22.22;      
           long l = 222222;
           String st = "Stringtest2";
           
           p.setTitle(title);
           p.setDescription(description);
           p.setD(d);
           p.setL(l);
           p.setSt(st);
           
           results.add(p);
           **/

           
           return results;
       }
       
        @WebResult(name = "parameter")
        public Collection<Param> getCollection(Collection<Param> param) {  
               /** 
               Param p = new Param();      
               int title = 222;
               String description = "Description2";
               double d = 22.22;      
               long l = 222222;
               String st = "Stringtest2";
               
               p.setTitle(title);
               p.setDescription(description);
               p.setD(d);
               p.setL(l);
               p.setSt(st);
               
               param.add(p);
               **/

            return param;
        }  
    }
     
    Jetzt die dazugehörige Bean:
    Code (Java):

    package test;

    import javax.jws.WebService;

    @WebService
    public class Param{
       
        int title;
        String description;
        double d;      
        long l;
        String st;

        public Param() {
            // empty constructor
        }

        public Param(int title, String description, double d, long l, String st) {
            this.title = title;
            this.description = description;
            this.d = d;
            this.l = l;
            this.st = st;
        }

        public int getTitle(){return title;}
        public void setTitle(int title){this.title = title;}
       
        public String getDescription(){return description;}
        public void setDescription(String description){this.description = description;}

        public double getD(){return d;}
        public void setD(double d){this.d = d;}

        public long getL(){return l;}
        public void setL(long l){this.l = l;}

        public String getSt(){return st;}
        public void setSt(String st){this.st = st;}
    }
     
    ... und die Startklasse:
    Code (Java):

    package test;

    import javax.xml.ws.Endpoint;


    public class Server {

        public static void main (String args[]) {
            Hello server = new Hello();    
            Endpoint.publish("http://localhost:8080/Server", server);
        }
    }
     
    und der Request:
    [XML]
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://test/">
    <soapenv:Header/>
    <soapenv:Body>
    <test:testArrayList>
    <arg0>
    <d>12</d>
    <description>Description</description>
    <l>66</l>
    <st>Stringtest</st>
    <title>1</title>
    </arg0>
    </test:testArrayList>
    </soapenv:Body>
    </soapenv:Envelope>
    [/XML]

    und die Response:
    [XML]
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
    <ns2:testArrayListResponse xmlns:ns2="http://test/">
    <return>
    <d>12.0</d>
    <description>Description</description>
    <l>66</l>
    <st>Stringtest</st>
    <title>1</title>
    </return>
    </ns2:testArrayListResponse>
    </S:Body>
    </S:Envelope>
    [/XML]
     
  3. waldek87

    waldek87 Neues Mitglied

    Probier es mal mit dem List anstatt ArrayList als Rückgabetyp
     
  4. Frank04

    Frank04 Neues Mitglied

    Eine ArrayList wird bei der Übertragung automatisch zur List. Der Client muss auch die erhaltenen Daten in eine List speichern.

    Hast Du die Validation der Nachrichten an?

    Code (Java):
    @WebService  
    @SchemaValidation()
    Auf einer Tomcat7 und einer Websphere 6.1 Implementierung funktioniert die Listenübergabe.
     
    Zuletzt bearbeitet: 4. Apr. 2012
Die Seite wird geladen...