IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Services Web Java Discussion :

Web service retourne "exception"


Sujet :

Services Web Java

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 8
    Points : 5
    Points
    5
    Par défaut Web service retourne "exception"
    Bonjour, voilà mon problème, après avoir galérer un moment j'ai écris un petit client web services mais comme retour j'ai "exception".

    Voici mon code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    try {
    				String endpoint2 = "http://www.webservicex.net/stockquote.asmx";
     
    				Service  service = new Service();
    				Call     call    = (Call) service.createCall();
     
    				call.setTargetEndpointAddress(new URL(endpoint2) );
    				call.setOperationName(new QName("http://www.webservicex.net/stockquote.asmx?wsdl", "GetQuote"));
    				call.setSOAPActionURI("http://www.webserviceX.NET/GetQuote");
     
    				String ret = (String) call.invoke(new Object[] { "edf" });
     
    				System.out.println("Sent 'ibm', Got '" + ret + "'");
     
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    et l'affichage :

    Sent 'ibm', Got 'exception'

    Or je devrait pas du tout avoir ça.
    Merci si vous pouvez m'aider !

  2. #2
    oca
    oca est déconnecté
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    354
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2004
    Messages : 354
    Points : 421
    Points
    421
    Par défaut
    A mon avis, to QName n'est pas bon...

    je verrais plutôt qqch comme :
    call.setOperationName(new QName("http://www.webserviceX.NET/", "GetQuote"));

    le namespace des operations (ici "http://www.webserviceX.NET/") se trouve dans la definition de la wsdl,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <wsdl:definitions ... targetNamespace="http://www.webserviceX.NET/" ...
    a+

  3. #3
    Membre actif
    Inscrit en
    Juin 2008
    Messages
    207
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 207
    Points : 215
    Points
    215
    Par défaut
    La suggestion d'oca me paraissait la bonne, mais en créant le même client, je m'aperçois que cela ne fonctionne pas...

    Sinon, tu peux utiliser ce client (la classe StockQuoteSoapProxy est générée en créant un client service web avec Eclipse ou NetBeans en passant à l'IDE le contrat WSDL du service web) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import java.rmi.RemoteException;
     
    import NET.webserviceX.www.StockQuoteSoapProxy;
     
    public class StockQuoteClient {
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		try {
    			StockQuoteSoapProxy proxy = new StockQuoteSoapProxy();
    			String ret = proxy.getQuote("edf");
     
    			System.out.println("Sent 'edf', Got '" + ret + "'");
    		} catch(RemoteException e) {
    			e.printStackTrace();
    		}
    	}
    }
    Néanmoins, le client avec Axis devrait pouvoir fonctionner également...

  4. #4
    Membre actif
    Inscrit en
    Juin 2008
    Messages
    207
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 207
    Points : 215
    Points
    215
    Par défaut
    En fait l'erreur provient du paramètre "edf" qui n'est pas transmis au service web lors de son invocation :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String ret = (String) call.invoke(new Object[] { "edf" });
    C'est une problématique qui m'intéresse également, alors si quelqu'un a une explication...

    EDIT : Voici un lien (en anglais) qui détaille un peu mieux ce problème, mais malheureusement sans le résoudre : cliquez ici

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 8
    Points : 5
    Points
    5
    Par défaut
    Le problème c'est que j'ai écrit ça (en fait je l'ai trouvé mais je ne sais plus où) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    String endpoint = "http://www.ebob42.com/cgi-bin/Romulan.exe/soap/IRoman";     
    		    if (args.length != 1) {
    		      System.out.println("Usage: ClientRoman argEntier");
    		      return;
    		    }       
    		    Integer entier = new Integer(args[0]);
     
    		    Service  service = new Service();
    		    Call call = (Call) service.createCall();
    		    call.setTargetEndpointAddress( new java.net.URL(endpoint) );
    		    call.setSOAPActionURI("urn:Roman-IRoman#IntToRoman");
    		    call.setOperationName( new QName("urn:Roman-IRoman", "IntToRoman") );
    		    call.addParameter( "Int", XMLType.XSD_INT, ParameterMode.IN );
    		    call.setReturnType( XMLType.XSD_STRING );
     
    		    String result = (String) call.invoke( new Object [] { entier });
     
    		    System.out.println("IntToRoman("+entier+") = "+result);
    Avec une réponse correct : IntToRoman(2) = II

    Donc là l'entier passe bien le "invoke".

  6. #6
    oca
    oca est déconnecté
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    354
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2004
    Messages : 354
    Points : 421
    Points
    421
    Par défaut
    C'est bizarre cette histoire...
    J'ai essayé ton premier programme avec l'operation GetQuote),
    et j'ai la même erreur que toi, par contre, si j'utilise le "Web service explorer" d'Eclipse pour accéder au service, ça marche bien.

    A noter que ce service utilise un binding document/literal alors que
    le service avec les chiffres romain utilise du rpc/encoded

    Je vais continuer de chercher un peu
    A+

  7. #7
    Membre actif
    Inscrit en
    Juin 2008
    Messages
    207
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 207
    Points : 215
    Points
    215
    Par défaut
    On peut rajouter ces deux lignes pour spécifier l'utilisation d'un binding document/literal :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    call.setOperationUse(Use.LITERAL);
    call.setOperationStyle(Style.DOCUMENT);
    Toutefois, l'utilisation d'un binding document provoque l'exception suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    AxisFault
     faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
     faultSubcode: 
     faultString: org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
     faultActor: 
     faultNode: 
     faultDetail: 
    	{http://xml.apache.org/axis/}stackTrace:org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
    	at org.apache.axis.encoding.ser.SimpleDeserializer.onStartChild(SimpleDeserializer.java:145)
    	at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
    	at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
    	at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
    	at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
    	at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
    	at org.apache.axis.client.Call.invoke(Call.java:2467)
    	at org.apache.axis.client.Call.invoke(Call.java:2366)
    	at org.apache.axis.client.Call.invoke(Call.java:1812)
    	at client.HelloClientWithAxis.main(HelloClientWithAxis.java:78)
     
    	{http://xml.apache.org/axis/}hostname:WXP-RMARTIN
     
    org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
    	at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
    	at org.apache.axis.client.Call.invoke(Call.java:2470)
    	at org.apache.axis.client.Call.invoke(Call.java:2366)
    	at org.apache.axis.client.Call.invoke(Call.java:1812)
    	at client.HelloClientWithAxis.main(HelloClientWithAxis.java:78)
    Caused by: org.xml.sax.SAXException: SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
    	at org.apache.axis.encoding.ser.SimpleDeserializer.onStartChild(SimpleDeserializer.java:145)
    	at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
    	at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
    	at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
    	at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
    	at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
    	at org.apache.axis.client.Call.invoke(Call.java:2467)
    	... 3 more

  8. #8
    oca
    oca est déconnecté
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    354
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2004
    Messages : 354
    Points : 421
    Points
    421
    Par défaut
    Peut-être qu'il faut ajouter les call.addParameter(...) et call.setReturnType(...) ?
    A+

  9. #9
    Membre actif
    Inscrit en
    Juin 2008
    Messages
    207
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 207
    Points : 215
    Points
    215
    Par défaut
    Déjà essayé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    try {
        Call call = (Call)new Service().createCall();
        String endpoint = "http://www.webservicex.net/stockquote.asmx";
     
        call.setTargetEndpointAddress(new URL(endpoint));
        call.setProperty(Call.SOAPACTION_USE_PROPERTY, true);
        call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://www.webservicex.net/GetQuote");
        call.setOperationName(new QName("http://www.webservicex.net", "GetQuote"));
        //call.setOperationStyle(Style.DOCUMENT);
        //call.setOperationUse(Use.LITERAL);
        call.addParameter("symbol", XMLType.XSD_STRING, String.class, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING, String.class);
     
        String symbol = "edf";
        String ret = (String)call.invoke(new Object[] {symbol});
     
        System.out.println("Sent '" + symbol + "', Got '" + ret + "'");
    } catch(MalformedURLException e) {
        e.printStackTrace();
    } catch(RemoteException e) {
        e.printStackTrace();
    } catch(ServiceException e) {
        e.printStackTrace();
    }
    J'ai tourné les différents setters de la classe Call et j'ai aussi essayé de manipuler les classes du package javax.xml.rpc plutôt que org.apache.axis.client, mais c'est toujours les mêmes résultats qui sortent :
    - soit le paramètre n'est pas pris en compte si l'on ne spécifie pas un binding de type document (un autre test avec un service web ASP.NET simple que j'ai créé le met en lumière)
    - soit le client envoie l'exception org.xml.sax.SAXException donnée précédemment si l'on spécifie un binding de type document

    Ces problèmes ont été plusieurs fois rencontrés, mais à chaque fois, aucune réponse n'a été donnée si ce n'est l'utilisation d'une invocation statique du service via un stub/proxy client généré par Eclipse, NetBeans ou encore WSDL2Java...

  10. #10
    oca
    oca est déconnecté
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    354
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2004
    Messages : 354
    Points : 421
    Points
    421
    Par défaut
    et avec le namespace http://www.webserviceX.NET/ ?
    (dans ton exemple je vois http://www.webservicex.net)
    A+

  11. #11
    Membre actif
    Inscrit en
    Juin 2008
    Messages
    207
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 207
    Points : 215
    Points
    215
    Par défaut
    Non, aucune différence...

    De toute façon, je teste en parallèle un service web ASP.NET simple en local et j'obtiens les mêmes types de résultats...

    Sinon, le binding document ne fonctionne également pas pour des services web déployés sous Axis et Axis2 :

    Le contrat WSDL d'un service HelloService déployé sous Axis :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions targetNamespace="http://localhost:8080/axis/HelloService.jws" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/HelloService.jws" xmlns:intf="http://localhost:8080/axis/HelloService.jws" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <!--WSDL created by Apache Axis version: 1.4
    Built on Apr 22, 2006 (06:55:48 PDT)-->
       <wsdl:message name="sayHelloResponse">
          <wsdl:part name="sayHelloReturn" type="xsd:string"/>
       </wsdl:message>
       <wsdl:message name="sayHelloRequest">
          <wsdl:part name="name" type="xsd:string"/>
       </wsdl:message>
       <wsdl:portType name="HelloService">
          <wsdl:operation name="sayHello" parameterOrder="name">
             <wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
             <wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
          </wsdl:operation>
       </wsdl:portType>
       <wsdl:binding name="HelloServiceSoapBinding" type="impl:HelloService">
          <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
          <wsdl:operation name="sayHello">
             <wsdlsoap:operation soapAction=""/>
             <wsdl:input name="sayHelloRequest">
                <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>
             </wsdl:input>
             <wsdl:output name="sayHelloResponse">
                <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/HelloService.jws" use="encoded"/>
             </wsdl:output>
          </wsdl:operation>
       </wsdl:binding>
       <wsdl:service name="HelloServiceService">
          <wsdl:port binding="impl:HelloServiceSoapBinding" name="HelloService">
             <wsdlsoap:address location="http://localhost:8080/axis/HelloService.jws"/>
          </wsdl:port>
       </wsdl:service>
    </wsdl:definitions>
    Le code du client correspondant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    try {
        Call call = (Call)new Service().createCall();
        String endpoint = "http://localhost:8080/axis/HelloService.jws";
     
        call.setTargetEndpointAddress(new URL(endpoint));
        call.setProperty(Call.SOAPACTION_USE_PROPERTY, true);
        call.setProperty(Call.SOAPACTION_URI_PROPERTY, "");
        call.setOperationName(new QName("http://localhost:8080/axis/HelloService.jws", "sayHello"));
        call.setOperationStyle(Style.DOCUMENT);
        call.setOperationUse(Use.LITERAL);
        call.addParameter("name", XMLType.XSD_STRING, String.class, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING, String.class);
     
        String name = "Christophe";
        String message = (String)call.invoke(new Object [] {name});
     
        System.out.println(message);
    } catch(MalformedURLException e) {
        e.printStackTrace();
    } catch(RemoteException e) {
        e.printStackTrace();
    } catch(ServiceException e) {
        e.printStackTrace();
    }
    L'exception levée :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    AxisFault
     faultCode: {http://xml.apache.org/axis/}Client
     faultSubcode: 
     faultString: No such operation 'name'
     faultActor: 
     faultNode: 
     faultDetail: 
    	{http://xml.apache.org/axis/}hostname:WXP-RMARTIN
     
    No such operation 'name'
    	at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
    	at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
    	at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
    	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    	at javax.xml.parsers.SAXParser.parse(Unknown Source)
    	at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
    	at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
    	at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
    	at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
    	at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
    	at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
    	at org.apache.axis.client.Call.invoke(Call.java:2767)
    	at org.apache.axis.client.Call.invoke(Call.java:2443)
    	at org.apache.axis.client.Call.invoke(Call.java:2366)
    	at org.apache.axis.client.Call.invoke(Call.java:1812)
    	at client.HelloClientWithAxis.main(HelloClientWithAxis.java:34)
    Le contrat WSDL d'un service hello déployé sous Axis2 :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions name="Hello" targetNamespace="http://localhost:8080/Hello/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://localhost:8080/Hello/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
      <wsdl:types>
        <xsd:schema targetNamespace="http://localhost:8080/Hello/">
          <xsd:element name="sayHello">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="name" type="xsd:string"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="sayHelloResponse">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="message" type="xsd:string"/>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>
     
        </xsd:schema>
      </wsdl:types>
      <wsdl:message name="sayHelloRequest">
        <wsdl:part name="parameters" element="tns:sayHello">
        </wsdl:part>
      </wsdl:message>
      <wsdl:message name="sayHelloResponse">
        <wsdl:part name="parameters" element="tns:sayHelloResponse">
        </wsdl:part>
      </wsdl:message>
      <wsdl:portType name="Hello">
        <wsdl:operation name="sayHello">
          <wsdl:input message="tns:sayHelloRequest">
        </wsdl:input>
          <wsdl:output message="tns:sayHelloResponse">
        </wsdl:output>
        </wsdl:operation>
      </wsdl:portType>
      <wsdl:binding name="HelloSOAP" type="tns:Hello">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="sayHello">
          <soap:operation soapAction="http://localhost:8080/Hello/sayHello"/>
          <wsdl:input>
            <soap:body use="literal"/>
          </wsdl:input>
          <wsdl:output>
            <soap:body use="literal"/>
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="Hello">
        <wsdl:port name="HelloSOAP" binding="tns:HelloSOAP">
          <soap:address location="http://10.63.0.52:8080/axis2/services/Hello"/>
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>
    Le code du client correspondant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    try {
        Call call = (Call)new Service().createCall();
        String endpoint = "http://localhost:8080/axis2/services/Hello";
     
        call.setTargetEndpointAddress(new URL(endpoint));
        call.setProperty(Call.SOAPACTION_USE_PROPERTY, true);
        call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://localhost:8080/Hello/sayHello");
        call.setOperationName(new QName("http://localhost:8080/Hello/", "sayHello"));
        call.setOperationStyle(Style.DOCUMENT);
        call.setOperationUse(Use.LITERAL);
        call.addParameter("name", XMLType.XSD_STRING, String.class, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_STRING, String.class);
     
        String name = "Christophe";
        String message = (String)call.invoke(new Object [] {name});
     
        System.out.println(message);
    } catch(MalformedURLException e) {
        e.printStackTrace();
    } catch(RemoteException e) {
        e.printStackTrace();
    } catch(ServiceException e) {
        e.printStackTrace();
    }
    L'exception levée :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    AxisFault
     faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server
     faultSubcode: 
     faultString: org.apache.axis2.databinding.ADBException: Unsupported type http://www.w3.org/2001/XMLSchema string
     faultActor: 
     faultNode: 
     faultDetail: 
    	{http://xml.apache.org/axis/}stackTrace:org.apache.axis2.databinding.ADBException: Unsupported type http://www.w3.org/2001/XMLSchema string
    	at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
    	at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
    	at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
    	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
    	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    	at javax.xml.parsers.SAXParser.parse(Unknown Source)
    	at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
    	at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
    	at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
    	at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
    	at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
    	at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
    	at org.apache.axis.client.Call.invoke(Call.java:2767)
    	at org.apache.axis.client.Call.invoke(Call.java:2443)
    	at org.apache.axis.client.Call.invoke(Call.java:2366)
    	at org.apache.axis.client.Call.invoke(Call.java:1812)
    	at client.HelloClientWithAxis.main(HelloClientWithAxis.java:60)
    Pourtant, si je ne spécifie pas de binding document, tout fonctionne correctement, et sans même à avoir à indiquer toutes les propriétés de l'objet call...

  12. #12
    oca
    oca est déconnecté
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    354
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2004
    Messages : 354
    Points : 421
    Points
    421
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
     
    package oca;
     
    import java.net.URL;
     
    import javax.xml.namespace.QName;
     
    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    import org.apache.axis.description.OperationDesc;
    import org.apache.axis.description.ParameterDesc;
     
    public class Test {
     
    	public static void main(String[] args) {
    		try {
     
    		    OperationDesc oper;
    	        ParameterDesc param;
    	        oper = new OperationDesc();
    	        oper.setName("GetQuote");
    	        param = new ParameterDesc(
    	        		new QName("http://www.webserviceX.NET/", "symbol"), 
    	        		ParameterDesc.IN, 
    	        		new QName("http://www.w3.org/2001/XMLSchema", "string"), 
    	        		java.lang.String.class, false, false);
     
    	        oper.addParameter(param);
    	        oper.setReturnType(new QName("http://www.w3.org/2001/XMLSchema", "string"));
     
    	        Call _call = (Call)new Service().createCall();
    	        String endpoint = "http://www.webservicex.net/stockquote.asmx";
    		    _call.setTargetEndpointAddress(new URL(endpoint));
    	        _call.setOperation(oper);
    	        _call.setSOAPActionURI("http://www.webserviceX.NET/GetQuote");
    	        _call.setOperationName(new QName("http://www.webserviceX.NET/", "GetQuote"));
     
    		    String symbol = "edf";
    		    String ret = (String)_call.invoke(new Object[] {symbol});
    		    System.out.println("Sent '" + symbol + "', Got '" + ret + "'");
     
    		} catch(Exception e) {
    		    e.printStackTrace();
    		}
    	}
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Sent 'edf', Got '<StockQuotes><Stock><Symbol>EDF</Symbol><Last>12.72</Last><Date>6/25/2008</Date><Time>1:51pm</Time><Change>-0.02</Change><Open>12.68</Open><High>12.84</High><Low>12.68</Low><Volume>49400</Volume><MktCap>310.8M</MktCap><PreviousClose>12.74</PreviousClose><PercentageChange>-0.16%</PercentageChange><AnnRange>11.12 - 13.58</AnnRange><Earns>0.951</Earns><P-E>13.40</P-E><Name>WESTERN ASSET EME</Name></Stock></StockQuotes>'
    pffff.... dur la vie
    A+

  13. #13
    Membre actif
    Inscrit en
    Juin 2008
    Messages
    207
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 207
    Points : 215
    Points
    215
    Par défaut
    Merci.

    Pour le service StockQuote (.NET), l'utilisation d'un objet OperationDesc semble nécessaire, les méthodes addParameter de l'objet Call pourtant a priori équivalentes ne sont pas suffisantes.

    Par contre, pour un service simple Hello en .NET, passer par un objet OperationDesc n'est pas forcément requis, il fallait simplement définir l'opération avec un QName ("http://tempuri.org/", "name") et non pas avec une String "name".

    À l'inverse, pour un service Axis2, le targetNamespace ne doit pas être indiqué, sous peine de voir ses arguments non passés ou bien une exception levée (selon le type de binding utilisé pour le service : ADB, JiBX, JAXB, XMLBeans,...).

    Enfin, je me demande pourquoi il n'est pas utile de spécifier un style document (avec par exemple une ligne call.setOperationStyle(Style.DOCUMENT); ) alors que le style par défaut est RPC...

    D'ailleurs, si on écrit cette ligne, elle doit être placée avant call.setOperation(oper); sinon une exception est là encore levée...

  14. #14
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 8
    Points : 5
    Points
    5
    Par défaut
    Pourrais-je savoir à quoi correspondent ParameterDesc et OperationDesc. Sont-ce seulement des descripteurs d'opération et parametre. A quoi correspond la chaine de caractère "symbol" dans le constructeur du QName du constructeur de ParameterDesc ? Car là il existe quelques flous, mon but étant bien sur d'essayer d'autres services d'autres sites.
    Sinon, qu'utilisez-vous pour utiliser un service web en Java ?

    En tout cas merci et un grand bravo.

  15. #15
    oca
    oca est déconnecté
    Membre averti
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    354
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : Suisse

    Informations forums :
    Inscription : Octobre 2004
    Messages : 354
    Points : 421
    Points
    421
    Par défaut
    symbol est le nom du paramètre, on le vois dans la wsdl...

    Si j'affiche un extrait de la wsdl (à l'envers pour partir du service...)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
      <wsdl:service name="StockQuote">
      <wsdl:port binding="tns:StockQuoteSoap" name="StockQuoteSoap">
      <soap:address location="http://www.webservicex.net/stockquote.asmx" /> 
      </wsdl:port>
    	...
    	...
      </wsdl:service>
      
      <wsdl:binding name="StockQuoteSoap" type="tns:StockQuoteSoap">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
    - <wsdl:operation name="GetQuote">
      <soap:operation soapAction="http://www.webserviceX.NET/GetQuote" style="document" /> 
      
      <wsdl:portType name="StockQuoteSoap">
    	<wsdl:operation name="GetQuote">
      <documentation xmlns="http://schemas.xmlsoap.org/wsdl/">Get Stock quote for a company Symbol</documentation> 
      <wsdl:input message="tns:GetQuoteSoapIn" /> 
      <wsdl:output message="tns:GetQuoteSoapOut" /> 
      </wsdl:operation>
      </wsdl:portType>
      
      
      <wsdl:message name="GetQuoteSoapIn">
      <wsdl:part element="tns:GetQuote" name="parameters" /> 
      </wsdl:message>
      
      <s:element name="GetQuote">
     <s:complexType>
     <s:sequence>
      <s:element maxOccurs="1" minOccurs="0" name="symbol" type="s:string" /> 
      </s:sequence>
      </s:complexType>

    Ceci permet d'avoir la requête soap suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    <soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:q0="http://www.webserviceX.NET/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     
    <soapenv:Body>
     <q0:GetQuote>
      <q0:symbol>edf</q0:symbol> 
      </q0:GetQuote>
      </soapenv:Body>
     </soapenv:Envelope>
    et la réponse :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    <soap:Envelope 
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <soap:Body>
     <GetQuoteResponse xmlns="http://www.webserviceX.NET/">
      <GetQuoteResult><StockQuotes><Stock><Symbol>EDF</Symbol><Last>12.57</Last><Date>6/26/2008</Date><Time>1:23pm</Time>
      <Change>-0.06</Change><Open>12.63</Open><High>12.63</High><Low>12.52</Low><Volume>36163</Volume>
      <MktCap>307.1M</MktCap><PreviousClose>12.63</PreviousClose><PercentageChange>-0.48%</PercentageChange>
      <AnnRange>11.12 - 13.58</AnnRange><Earns>0.951</Earns><P-E>13.28</P-E><Name>WESTERN ASSET EME</Name>
      </Stock></StockQuotes></GetQuoteResult> 
      </GetQuoteResponse>
      </soap:Body>
      </soap:Envelope>
    A+

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Appel d'un web service retournant un objet complexe
    Par yayasam dans le forum Services Web
    Réponses: 5
    Dernier message: 24/03/2011, 11h48
  2. Réponses: 6
    Dernier message: 01/03/2010, 09h53
  3. Réponses: 5
    Dernier message: 03/04/2009, 10h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo