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

Now your setting in the Web Service screen should be like the following. Select “Publish the Web Service” and click Finish.

          

Once this operation is complete, we will have new elements in our workspace.

Now, that we have our Web Service running you can access the auto generated WSDL file.

If you access to the following link: http://localhost:8080/SOAPDemo/services/Demo?wsdl you should see a WSDL file but in 1.1 version. Since we have targeted our tutorial to WSDL 2.0, let’s access the same URL but adding a parameter to show the WSDL in 2.0 versions: http://localhost:8080/SOAPDemo/services/Demo?wsdl=wsdl2.0 and it should contain something similar to the following:

<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 = "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: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: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: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: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>

Now that we have seen that our WSDL is exposing our web services, we can use another tool to test it.

I particularly use SOAP UI when dealing with web services, so I can see what the request and responses looks like, so Download and install SOAP UI from http://www.soapui.org/

Open SOAP UI and Create a new project with the WSDL from our service: http://localhost:8080/SOAPDemo/services/Demo?wsdl.

          

This action will create a new project, and display all available interfaces and operations for our web service. You should see something similar to this, displaying the returnMessage method we have created in our Demo class.

          

Double click on the Request1 or right click on the returnMessage operation and create a new request.

          

The request should have an already detailed Envelope for our XML message and it should look like this:

<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:goj = "http://goj.demo.com">
   <soapenv:Header/>
   <soapenv:Body>
      <goj:returnMessage>
         <goj:name>?</goj:name>
      </goj:returnMessage>
   </soapenv:Body>
</soapenv:Envelope>

Let’s now define something in our <goj:name> element like: John Smith. If we look back into our Demo class, we have defined that the returnMessage method should be receiving a name as parameter, this is that name.

<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:goj = "http://goj.demo.com">
   <soapenv:Header/>
   <soapenv:Body>
      <goj:returnMessage>
         <goj:name>John Smith</goj:name>
      </goj:returnMessage>
   </soapenv:Body>
</soapenv:Envelope>

Like us on Facebook