Web Services - One stop shop

Technologies required for creating Web services: XSD, JAXB, WSDL, JAX-WS annotations

WSDL - Web Service Descriptor Language. Using WSDL, you can register your web service on a server. 

We can write WSDL manually or use annotations to convert a JAVA file into a WSDL file. 2nd option is the easiest and the preferred way.
Here, I am demonstrating how @Webservice annotation converts java interfaces and classed to a WSDL is given below

On server, we write interfaces and Impls for every service. When we deploy the service with @Webservice annotation. The service gets registered with the  Impl name.

WSDL components:
WSDL has the below components
1. Service
2. Port
3. Binding
4. PortType
5. Operation
6. Messages
7. Types

So, A WSDL roughly looks like this..
<Definitions>
  <Type/>or XSD import
  <Messages/>
<PortType><Operation/></PortType>
<Binding/>
<Service>
         <Port binding =""> <SOAPAddress location=""> </Port>
</Service>
</Definitions>


Here is how annotations convert your class to an equivalent WSDL xml.
service is represented as <ClassName>Service. It has port 

port is represented as <ClassName>Port.
port has bindings link (how the service can be accessed) and SOAP address (where the service is located).

binding is represented as <Port>Binding. Binding talks about how the web services can be accessed and the format of request/response message(DOC/RPC).

portType and operation are represented as the <Class Name> and <Method Name> of your web service implementation class. portType has Operations.

operation has input, output and fault messages. Irrespective of the no of args a method has in java class. WSDL bundles them as a single message.

i/p message is represented as <operation> name and o/p is represented as <operation>Response message. Fault name is the exception name that the method throws

Messages have types - Can be described in RPC/DOC



Binding Types:
RPC - RPC does not have a XSD. IT supports inline XML for types in WSDL. As there is no schema, the data cannot be validated in case of RPC style binding.

Document - It maintains a seperate XSD and import that XSD to main WSDL. All the advantages that XSD provides can be well utilised with this approach.
i.e Document structure, Data types, request and validations.

What is XSD?
It is XML schema definition, which defines the structure of the entire XML and the data types that each element in the XML document support

Web Service creation:
Bottom to Top Approach   -  Creating Webservices by starting with JAVA interface creation and generate an WSDL out of java interface.
Disadvantage with this approach is whenever we change the interface (class/method name). All the clients who consumes our web service gets impacted

Top to Bottom Approach - Creating webservices by starting with WSDL first and generating the JAVA interface out of the WSDL. 
This approach is the best way of creating a web service. Because, with this way we can fix the interface that can be  shared it to the Web service consumers.
As both the web service creation and consumption are based out of the WSDL. Neither parties(Consumer and Provider) get impacted.

The biggest chanllenge with this approach is to get hands-on on WSDL for Creating the web service. But, we have an alternative which works almost the same way as 
what this approach says.

So the alternative way is to create a WSDL from a very basic java interface and customise the generated WSDL and share it to both consumer and provider.

Here, customizing the generated WSDL can be done the help of JAX-WS annotations by customizing the name of web service interfaces & name of methods, name of binding
etc., Because, the container generates the WSDL based on the annotated names, changing the class or method names at the provider end does not have any impact on WSDL.

Eg: The WSDL generated out from the below snippet contains annotated names as service, port, binding, portTye and operations,  not the class file and method names. 


 @WebService(name="BlogspotCatalogue",     targetNamespace="http://javarephrased.blogspot.com")  
 @SOAPBinding(style= Style.DOCUMENT, use=Use.LITERAL)  
 public interface CatalogManagerInterface {  
      @WebMethod(action="fetch", operationName="fetchCatalogues")  
      @WebResult(partName="catalogueList")  
      public List<Catalogue> getCatogues(@WebParam(partName="searchString")String inputName)  
      throws InvalidInputException;  
 }  

Comments

Popular posts from this blog

Distributed database design using CAP theorem

SQL Analytical Functions - Partition by (to split resultset into groups)

Easy approach to work with files in Java - Java NIO(New input output)