Search code examples
javaxmlweb-servicessoapsaaj

How to deal with a method in a SOAP-Call and its parameters


the API says I have to use the method "getStock" and following parameters: accessToken,company,itemNumber,commissionNumber. I wrote this code but it doesn't work.

    SOAPEnvelope envelope = soapPart.getEnvelope();
    //envelope.addNamespaceDeclaration("sam", "http://samples.axis2.techdive.in");

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement method = soapBody.addChildElement("getStock");
    SOAPElement firstParam = method.addChildElement("accessToken");
    firstParam.addTextNode("xxx");

    SOAPElement secondParam = method.addChildElement("company");
    secondParam.addTextNode("AS");

    SOAPElement thirdParam = method.addChildElement("itemNumber");
    thirdParam.addTextNode("020001");

    SOAPElement fourthParam = method.addChildElement("commissionNumber");
    fourthParam.addTextNode("0");

    soapMessage.saveChanges();

And what about the NamespaceDeclaration?

I get this error:

Response SOAP Message = ns2:Client Cannot find dispatch method for {}getStock Process finished with exit code 0

Greetings Andrew


Solution

  • In order to specify the prefix and the name space, you can use a Qname object (import javax.xml.namespace.QName;) like this:

    QName stockQname = new QName("http://your_namespace_uri.com",
                    "getStock", "prefix");
    

    Change the prefix to your actual prefix.

    Change the namespace to your actual namespaceuri

    I rewrote your code to use Qnames:

    private static void test() throws SOAPException {
    
            MessageFactory factory = MessageFactory
                    .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
            SOAPMessage message = factory.createMessage();
            SOAPPart soapPart = message.getSOAPPart();
    
            SOAPEnvelope envelope = soapPart.getEnvelope();
    
            SOAPBody soapBody = envelope.getBody();
    
            QName stockQname = new QName("http://your_namespace_uri.com",
                    "getStock", "prefix");
            SOAPBodyElement stockElement = soapBody.addBodyElement(stockQname);
    
            QName accessQname = new QName("accessToken");
            SOAPElement accessElement = stockElement.addChildElement(accessQname);
            accessElement.addTextNode("xxx");
    
            QName companyQname = new QName("company");
            SOAPElement companyElement = stockElement.addChildElement(companyQname);
            companyElement.addTextNode("AS");
    
            QName itemQname = new QName("itemNumber");
            SOAPElement itemElement = stockElement.addChildElement(itemQname);
            itemElement.addTextNode("020001");
    
            QName commisionQname = new QName("commissionNumber");
            SOAPElement commissionElement = stockElement
                    .addChildElement(commisionQname);
            commissionElement.addTextNode("0");
    
            message.saveChanges();
    }
    

    And this is the generated SOAP message:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
      <SOAP-ENV:Header/>
      <SOAP-ENV:Body>
        <prefix:getStock xmlns:prefix="http://your_namespace_uri.com">
          <accessToken>xxx</accessToken>
          <company>AS</company>
          <itemNumber>020001</itemNumber>
          <commissionNumber>0</commissionNumber>
        </prefix:getStock>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    

    I am using this SAAJ library

    <dependency>
        <groupId>com.sun.xml.messaging.saaj</groupId>
        <artifactId>saaj-impl</artifactId>
        <version>1.3.25</version>
    </dependency>
    

    If you want to use SOAP 1.2 message protocol, simply change this line:

    MessageFactory factory = MessageFactory
                    .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
    

    To this line:

    MessageFactory factory = MessageFactory
                    .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
    

    Hope this helps