JAX-WS Tutorial: Precise WSDL

I know this is very simple web service. But I would like to highlight the importance of the use of JAX-WS annotations to get rid of the meaningless WSDL which is the main document for the web service consumer. I think, precise WSDL is advantage to maximum use of the web service.

Here the simple class

package com.ojitha.ws.second;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService(name = "Warehouse", serviceName = "WarehouseService", targetNamespace = "http://www.ojitha.com/stock")
@SOAPBinding(style = Style.RPC)
public class WarehouseImpl {
	@WebResult(name = "totalStock")
	public int getInventory(@WebParam(name = "firstNumber") int stockOnHand,
			@WebParam(name = "secondNumber") int stockToDeliver) {
		return stockOnHand + stockToDeliver;
	}
}

Here the generated WSDL

<wsdl:definitions name="WarehouseService" targetNamespace="http://www.ojitha.com/stock" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.ojitha.com/stock" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <wsdl:message name="getInventoryResponse">
      <wsdl:part name="totalStock" type="xsd:int"/>
   </wsdl:message>
   <wsdl:message name="getInventory">
      <wsdl:part name="firstNumber" type="xsd:int"/>
      <wsdl:part name="secondNumber" type="xsd:int"/>
   </wsdl:message>
   <wsdl:portType name="Warehouse">
      <wsdl:operation name="getInventory">
         <wsdl:input message="tns:getInventory" name="getInventory"/>
         <wsdl:output message="tns:getInventoryResponse" name="getInventoryResponse"/>
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="WarehouseServiceSoapBinding" type="tns:Warehouse">
      <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="getInventory">
         <soap:operation soapAction="" style="rpc"/>
         <wsdl:input name="getInventory">
            <soap:body namespace="http://www.ojitha.com/stock" use="literal"/>
         </wsdl:input>
         <wsdl:output name="getInventoryResponse">
            <soap:body namespace="http://www.ojitha.com/stock" use="literal"/>
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="WarehouseService">
      <wsdl:port binding="tns:WarehouseServiceSoapBinding" name="WarehousePort">
         <soap:address location="http://localhost:8080/SecondWSExample/WarehouseService"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

Well,  compare the Java class and the WSDL contents:

  • In the Java class (line 9), the name parameter in the @WebService defines the portType (line 9) in the WSDL.
  • In the Java class (line 9), the serviceName parameter in the @WebService defines  the definitions name (line 1) in the WSDL.
  • In the Java class (line 9), the targetNamespace parameter in the @WebService defines the definitions targetNamespace (line 1) in the WSDL.
  • In the Java class (line 12), the name parameter in the @WebResult defines the name of the “getInventoryResonse” (line 3) in the WSDL.
  • In the Java class (line 13), the name parameters in the @WebParam define the part names of the “getInventory” message(line 6, 7) in the WSDL.

The request of the service is as follows

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://www.ojitha.com/stock">
   <soapenv:Header/>
   <soapenv:Body>
      <stoc:getInventory>
         <firstNumber>10</firstNumber>
         <secondNumber>20</secondNumber>
      </stoc:getInventory>
   </soapenv:Body>
</soapenv:Envelope>

and the response as follows

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:getInventoryResponse xmlns:ns1="http://www.ojitha.com/stock">
         <totalStock>30</totalStock>
      </ns1:getInventoryResponse>
   </soap:Body>
</soap:Envelope>

Your comments are well come.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python