05 - Web Service example in Java: Page 3 of 3

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

          

By clicking Finish, eclipse will create new elements in our workspace and start the web service in our tomcat instance and you could access the web service by accessing the WSDL file.

Open a web browser and enter the following address: http://localhost:8080/WebServiceDemo/services/WSDemo?wsdl .

You should see the WSDL document generated for our webservice and it should be something like this:

<wsdl:definitions xmlns:wsdl  =  "http://schemas.xmlsoap.org/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:http = "http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs = "http://www.w3.org/2001/XMLSchema" xmlns:mime = "http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12 = "http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace = "http://goj.demo.com">
   <wsdl:documentation>Please Type your service description here</wsdl:documentation>
   <wsdl:types>
        <xs:schema attributeFormDefault = "qualified" elementFormDefault = "qualified" targetNamespace = "http://goj.demo.com">
            <xs:element name = "helloWorld">
                  <xs:complexType>
                       <xs:sequence>
                           <xs:element minOccurs = "0" name = "name" nillable = "true" type = "xs:string"/>
                       </xs:sequence>
                  </xs:complexType>
            </xs:element>
            <xs:element name = "helloWorldResponse">
                  <xs:complexType>
                       <xs:sequence>
                            <xs:element minOccurs = "0" name = "return" nillable = "true" type = "xs:string"/>
                       </xs:sequence>
                  </xs:complexType>
           </xs:element>
        </xs:schema>
   </wsdl:types>
   <wsdl:message name = "helloWorldRequest">
       <wsdl:part name = "parameters" element = "ns:helloWorld"/>
  </wsdl:message>
  <wsdl:message name = "helloWorldResponse">
       <wsdl:part name = "parameters" element = "ns:helloWorldResponse"/>
  </wsdl:message>
  <wsdl:portType name = "WSDemoPortType">
       <wsdl:operation name = "helloWorld">
              <wsdl:input message = "ns:helloWorldRequest" wsaw:Action = "urn:helloWorld"/>
              <wsdl:output message = "ns:helloWorldResponse" wsaw:Action = "urn:helloWorldResponse"/>
       </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name = "WSDemoSoap11Binding" type = "ns:WSDemoPortType">
        <soap:binding transport = "http://schemas.xmlsoap.org/soap/http" style = "document"/>
              <wsdl:operation name = "helloWorld">
                     <soap:operation soapAction = "urn:helloWorld" style = "document"/>
                     <wsdl:input>
                           <soap:body use = "literal"/>
                     </wsdl:input>
                     <wsdl:output>
                           <soap:body use = "literal"/>
                     </wsdl:output>
              </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name = "WSDemoSoap12Binding" type = "ns:WSDemoPortType">
        <soap12:binding transport = "http://schemas.xmlsoap.org/soap/http" style = "document"/>
            <wsdl:operation name = "helloWorld">
                     <soap12:operation soapAction = "urn:helloWorld" style = "document"/>
                     <wsdl:input>
                           <soap12:body use = "literal"/>
                     </wsdl:input>
                     <wsdl:output>
                           <soap12:body use = "literal"/>
                     </wsdl:output>
           </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name = "WSDemoHttpBinding" type = "ns:WSDemoPortType">
        <http:binding verb = "POST"/>
        <wsdl:operation name = "helloWorld">
              <http:operation location = "helloWorld"/>
                     <wsdl:input>
                           <mime:content type = "application/xml" part = "parameters"/>
                     </wsdl:input>
                     <wsdl:output>
                           <mime:content type = "application/xml" part = "parameters"/>
                     </wsdl:output>
       </wsdl:operation>
  </wsdl:binding>
       <wsdl:service name = "WSDemo">
              <wsdl:port name = "WSDemoHttpSoap11Endpoint" binding = "ns:WSDemoSoap11Binding">
                     <soap:address location = "http://localhost:8080/WebServiceDemo/services/WSDemo.WSDemoHttpSoap11Endpoint/"/>
              </wsdl:port>
              <wsdl:port name = "WSDemoHttpSoap12Endpoint" binding = "ns:WSDemoSoap12Binding">
                     <soap12:address location = "http://localhost:8080/WebServiceDemo/services/WSDemo.WSDemoHttpSoap12Endpoint/"/>
              </wsdl:port>
              <wsdl:port name = "WSDemoHttpEndpoint" binding = "ns:WSDemoHttpBinding">
                     <http:address location = "http://localhost:8080/WebServiceDemo/services/WSDemo.WSDemoHttpEndpoint/"/>
              </wsdl:port>
       </wsdl:service>
</wsdl:definitions>

Now that we have our service running, we can start testing our web service. You can use SOAP UI for accessing web services in an easy way, so you can download and install SOAP UI from http://www.soapui.org/

Open SOAP UI and create a new SOAP project with the WSDL from our service: http://localhost:8080/WebServiceDemo/services/WSDemo?wsdl

          

In the project explorer, you should be seeing the method that we are exposing: helloWorld. If you double click on it, you should see a request already created with the parameters needed for the request:

<soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:goj = "http://goj.demo.com">
<soapenv:Header/>
  <soapenv:Body>
    <goj:helloWorld>
    <!--Optional:-->
    <goj:name>John Doe</goj:name>
    </goj:helloWorld>
  </soapenv:Body>
</soapenv:Envelope>

By sending this request, you should see a response with the greeting message we have created, and the response should look something like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
     <ns:helloWorldResponse xmlns:ns="http://goj.demo.com">
     <ns:return>Greetings John Doe! You have reached the web service!</ns:return>
     </ns:helloWorldResponse>
  </soapenv:Body>
</soapenv:Envelope>

Now that you have seen your web service working and returning the information that we have defined, we conclude our tutorial for web services.

Like us on Facebook