Forum Discussion

irxn's avatar
irxn
Occasional Contributor
10 years ago

How to get parameter of SOAP operation

HI,

 

I want to read the methods of a webservice and show the user the methods and parameters in java.

This is my code so far:

 

WsdlProject project = new WsdlProject();
WsdlInterface iface = WsdlInterfaceFactory.importWsdl(project,"http://www.webservicex.com/globalweather.asmx?WSDL", true)[0];

Iterator<com.eviware.soapui.model.iface.Operation> i = iface.getOperationList().iterator();
WsdlOperation op;
while (i.hasNext()) {
op = (WsdlOperation) i.next();
System.out.println("Operation: " + op.getName());
}

Is it possible the get the parameters for an operation?

Best regards,

Peter 

  • Hi Peter,

     

    I had a quick look and say you have a WSDL with operation defined like:

     

    ...

    <message name="GetInvoiceRequestMessage">
    <part name="invoiceRef" element="inv:getInvoice"></part>
    </message>
    <message name="GetInvoiceResponseMessage">
    <part name="invoiceDocument" element="inv:InvoiceDocument"></part>
    </message>
    <portType name="InvoicePortType">
    <documentation>Get An Invoice Using It's Invoice Number
    </documentation>
    <operation name="getInvoice">
    <input name="GetInvoiceInput" message="tns:GetInvoiceRequestMessage"/>
    <output name="GetInvoiceOutput" message="tns:GetInvoiceResponseMessage"/>
    </operation>
    </portType>
    <binding name="InvoicePortBinding" type="tns:InvoicePortType">
    <soap:binding style="document"
    transport="http://schemas.xmlsoap.org/soap/http" />
    <operation name="getInvoice">
    <soap:operation
    soapAction="http://soapui.cookbook.samples/getInvoiceRequest/request" />
    <input name="GetInvoiceInput">
    <soap:body use="literal" />
    </input>
    <output name="GetInvoiceOutput">
    <soap:body use="literal" />
    </output>
    </operation>
    </binding>

    ...

     

    Then adding these lines of code to your code's while loop:

     

    while (i.hasNext()) {

     op = (WsdlOperation) i.next();

     System.out.println("Operation: " + op.getName());

     MessagePart[] messageParts = op.getDefaultRequestParts();

     System.out.println("Request Message Part Name: "+messageParts[0].getName());

     System.out.println("Request Message Part Type: "+messageParts[0].getPartType());

     System.out.println("Request Message Part Description: "+messageParts[0].getDescription());

    }

     

    Gives output:

     

    Operation: getInvoice

    Request Message Part Name: invoiceRef

    Request Message Part Type: CONTENT

    Request Message Part Description: invoiceRef of type [{http://soapui.cookbook.samples/schema/invoice}InvoiceRefType]

     

    Is this something like what you want?

     

    Note, if you actually wanted the request, this can be generated by adding the following lines:

     

    WsdlOperation operation = (WsdlOperation) iface.getOperationByName( "getInvoice" );

    WsdlRequest request = operation.addNewRequest( "getInvoice Request" );

    System.out.println("Request String Content: "+operation.createRequest( true ).toString());

     

    Gives:

     

    Request String Content: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inv="http://soapui.cookbook.samples/schema/invoice">

       <soapenv:Header/>

       <soapenv:Body>

          <inv:getInvoice>

             <inv:invoiceNo>?</inv:invoiceNo>

          </inv:getInvoice>

       </soapenv:Body>

     

    Hope this helps,

    Cheers,

    Rup

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi Peter,

     

    I had a quick look and say you have a WSDL with operation defined like:

     

    ...

    <message name="GetInvoiceRequestMessage">
    <part name="invoiceRef" element="inv:getInvoice"></part>
    </message>
    <message name="GetInvoiceResponseMessage">
    <part name="invoiceDocument" element="inv:InvoiceDocument"></part>
    </message>
    <portType name="InvoicePortType">
    <documentation>Get An Invoice Using It's Invoice Number
    </documentation>
    <operation name="getInvoice">
    <input name="GetInvoiceInput" message="tns:GetInvoiceRequestMessage"/>
    <output name="GetInvoiceOutput" message="tns:GetInvoiceResponseMessage"/>
    </operation>
    </portType>
    <binding name="InvoicePortBinding" type="tns:InvoicePortType">
    <soap:binding style="document"
    transport="http://schemas.xmlsoap.org/soap/http" />
    <operation name="getInvoice">
    <soap:operation
    soapAction="http://soapui.cookbook.samples/getInvoiceRequest/request" />
    <input name="GetInvoiceInput">
    <soap:body use="literal" />
    </input>
    <output name="GetInvoiceOutput">
    <soap:body use="literal" />
    </output>
    </operation>
    </binding>

    ...

     

    Then adding these lines of code to your code's while loop:

     

    while (i.hasNext()) {

     op = (WsdlOperation) i.next();

     System.out.println("Operation: " + op.getName());

     MessagePart[] messageParts = op.getDefaultRequestParts();

     System.out.println("Request Message Part Name: "+messageParts[0].getName());

     System.out.println("Request Message Part Type: "+messageParts[0].getPartType());

     System.out.println("Request Message Part Description: "+messageParts[0].getDescription());

    }

     

    Gives output:

     

    Operation: getInvoice

    Request Message Part Name: invoiceRef

    Request Message Part Type: CONTENT

    Request Message Part Description: invoiceRef of type [{http://soapui.cookbook.samples/schema/invoice}InvoiceRefType]

     

    Is this something like what you want?

     

    Note, if you actually wanted the request, this can be generated by adding the following lines:

     

    WsdlOperation operation = (WsdlOperation) iface.getOperationByName( "getInvoice" );

    WsdlRequest request = operation.addNewRequest( "getInvoice Request" );

    System.out.println("Request String Content: "+operation.createRequest( true ).toString());

     

    Gives:

     

    Request String Content: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inv="http://soapui.cookbook.samples/schema/invoice">

       <soapenv:Header/>

       <soapenv:Body>

          <inv:getInvoice>

             <inv:invoiceNo>?</inv:invoiceNo>

          </inv:getInvoice>

       </soapenv:Body>

     

    Hope this helps,

    Cheers,

    Rup

    • rupert_anderson's avatar
      rupert_anderson
      Valued Contributor

      Also Peter, I am curious to know why you would like to generate this information for a WSDL? Is it part of some kind of documentation tool?

      Thanks,

      Rup

      • irxn's avatar
        irxn
        Occasional Contributor

        Hi,

         

        thanks for your answers, this goes in the right direction.

         

        I write a programm where the user gives a url to a wsdl, gets the parameter he has to set und makes the request. 

        All dynamic at runtime.

         

        I have another question. I want to make a call to a webservice and get the result. But I am not able to pass some arguments. How should I archive this?

         

         WsdlOperation op = iface.getOperationAt(1);
                        String soapVersion = iface.getSoapVersion().toString();
                        String opName = op.getName();
        
                        System.out.println("OPERATION:"+opName);
        
                        WsdlRequest req = op.getRequestAt(0);
        
                        System.out.println("REQUEST :"+req.getName());
        
                        System.out.println("The request content is ="+req.getRequestContent());
        
                        System.out.println("The action is ="+req.getAction());
        
                        req.setEndpoint("http://www.webservicex.com/globalweather.asmx?WSDL");
        
                        String result="";
        
                        WsdlSubmitContext wsdlSubmitContext = new WsdlSubmitContext(req);
                        wsdlSubmitContext.setProperty("CountryName", "Germany");
                        wsdlSubmitContext.setProperty("CityName", "Berlin");
        
                        wsdlSubmitContext.put("CountryName", "Germany");
                        wsdlSubmitContext.put("CityName", "Berlin");
                        WsdlSubmit<?> submit = (WsdlSubmit<?>) req.submit(wsdlSubmitContext, false);
        
        
                        Response response = submit.getResponse();
        
                        result = response.getContentAsString();
        
                        System.out.println("The result ="+result);

        It seems the parameters are ignored. How can I add parameters to a call?

         

        Best regards