08 - SOAP Web Service example in Java: Page 4 of 5

Now, let’s submit our SOAP Message and make a request. Click on the Submit button:

          

 

Upon this request, we should receive a new SOAP message with the response from our web service. If you recall, we have defined that the returnMessage should return a simple message saying “Hello” plus the name given as parameter, so in our example now, we should see a message saying: “Hello John Smith”. Once the request is completed, the following response was received.

<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd = "http://www.w3.org/2001/XMLSchema" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <returnMessageResponse xmlns = "http://goj.demo.com">
         <returnMessageReturn>Hello John Smith</returnMessageReturn>
      </returnMessageResponse>
   </soapenv:Body>
</soapenv:Envelope>

As you can see, the response has what we have defined as the return message of the returnMessage method.

Now let’s add a new method to our Demo class by modifying the Demo class to have the following:

package com.demo.goj;
public class Demo {
    public String returnMessage (String name)
    {
        return "Hello " + name;
    }
    public double returnPrice (int productID)
    {
        if (productID == 1)
            return 10;
        else if (productID == 2)
            return 20;
        else return -1;
    }
}

The returnPrice method will be returning different prices depending on the ProductID, this should be connecting to a database to retrieve the prices depending on the Product ID, but we will keep this implementation very simple.

Let’s recreate our web service with the steps we saw before. Right click on the project and create a new Web service. Once we have done that, let’s review the WSDL file and it should have something similar to this:

<wsdl2:description xmlns:wsdl2 = "http://www.w3.org/ns/wsdl" xmlns:ns1 = "http://org.apache.axis2/xsd" xmlns:ns = "http://goj.demo.com" xmlns:wsaw = "http://www.w3.org/2006/05/addressing/wsdl" xmlns:wrpc = "http://www.w3.org/ns/wsdl/rpc" xmlns:wsoap = "http://www.w3.org/ns/wsdl/soap" xmlns:tns = "http://goj.demo.com" xmlns:wsdlx = "http://www.w3.org/ns/wsdl-extensions" xmlns:xs = "http://www.w3.org/2001/XMLSchema" xmlns:whttp = "http://www.w3.org/ns/wsdl/http" targetNamespace = "http://goj.demo.com">
    <wsdl2:documentation>Please Type your service description here</wsdl2:documentation>
    <wsdl2:types>
        <xs:schema attributeFormDefault = "qualified" elementFormDefault = "qualified" targetNamespace = "http://goj.demo.com">
            <xs:element name = "returnPrice">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "productID" type = "xs:int"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name = "returnPriceResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "return" type = "xs:double"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name = "returnMessage">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "name" nillable = "true" type = "xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name = "returnMessageResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs = "0" name = "return" nillable = "true" type = "xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl2:types>
    <wsdl2:interface name = "ServiceInterface">
        <wsdl2:operation name = "returnMessage" style = "http://www.w3.org/ns/wsdl/style/rpc" wrpc:signature = "name #in return #return " pattern = "http://www.w3.org/ns/wsdl/in-out">
            <wsdl2:input element = "ns:returnMessage" wsaw:Action = "urn:returnMessage"/>
            <wsdl2:output element = "ns:returnMessageResponse" wsaw:Action = "urn:returnMessageResponse"/>
        </wsdl2:operation>
        <wsdl2:operation name = "returnPrice" style = "http://www.w3.org/ns/wsdl/style/rpc" wrpc:signature = "productID #in return #return " pattern = "http://www.w3.org/ns/wsdl/in-out">
            <wsdl2:input element = "ns:returnPrice" wsaw:Action = "urn:returnPrice"/>
            <wsdl2:output element = "ns:returnPriceResponse" wsaw:Action = "urn:returnPriceResponse"/>
        </wsdl2:operation>
    </wsdl2:interface>
    <wsdl2:binding name = "DemoSoap11Binding" interface = "tns:ServiceInterface" type = "http://www.w3.org/ns/wsdl/soap" wsoap:version = "1.1">
        <wsdl2:operation ref = "tns:returnMessage" wsoap:action = "urn:returnMessage">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
        <wsdl2:operation ref = "tns:returnPrice" wsoap:action = "urn:returnPrice">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
    </wsdl2:binding>
    <wsdl2:binding name = "DemoHttpBinding" interface = "tns:ServiceInterface" whttp:methodDefault = "POST" type = "http://www.w3.org/ns/wsdl/http">
        <wsdl2:operation ref = "tns:returnMessage" whttp:location = "returnMessage">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
        <wsdl2:operation ref = "tns:returnPrice" whttp:location = "returnPrice">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
    </wsdl2:binding>
    <wsdl2:binding name = "DemoSoap12Binding" interface = "tns:ServiceInterface" type = "http://www.w3.org/ns/wsdl/soap" wsoap:version = "1.2">
        <wsdl2:operation ref = "tns:returnMessage" wsoap:action = "urn:returnMessage">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
        <wsdl2:operation ref = "tns:returnPrice" wsoap:action = "urn:returnPrice">
            <wsdl2:input/>
            <wsdl2:output/>
        </wsdl2:operation>
    </wsdl2:binding>
    <wsdl2:service name = "Demo" interface = "tns:ServiceInterface">
        <wsdl2:endpoint name = "DemoHttpEndpoint" binding = "tns:DemoHttpBinding" address = "http://10.116.65.66:8080/SOAPDemo/services/Demo.DemoHttpEndpoint/"/>
        <wsdl2:endpoint name = "DemoHttpSoap11Endpoint" binding = "tns:DemoSoap11Binding" address = "http://10.116.65.66:8080/SOAPDemo/services/Demo.DemoHttpSoap11Endpoint/"/>
        <wsdl2:endpoint name = "DemoHttpSoap12Endpoint" binding = "tns:DemoSoap12Binding" address = "http://10.116.65.66:8080/SOAPDemo/services/Demo.DemoHttpSoap12Endpoint/"/>
    </wsdl2:service>
</wsdl2:description>

You can see that there are new types defined for returnPrice elements, new operations and new bindings in the latest WSDL document.

Now in SOAP UI, let’s remove the existing Demo project and re-create it, with the previous address: http://localhost:8080/SOAPDemo/services/Demo?wsdl

Like us on Facebook