Java and J2EE Tutorials, Jsp and Servlet Tutorials, Spring MVC, Solr, XML, JSON Examples, Hibernate & Struts 2 Hello World projects



Monday, 22 April 2013

Spring Web Services - What is WSDL, XSD and Contract First & Contract Last

In this particular blog we will dive into Web Services and other useful concepts concepts related to Web Services. We will come to know what is an Contract ?, What is WSDL ?, What is XSD and What is difference between Contract First and Contract Last Web Services.

What are Web Services ?

Web Services are xml based independent and self descriptive application components that are used to communicate with open protocols.

The core of Web Services is a combination of XML and HTTP (XML+HTTP), XML is used as a common language for communication in between different platforms and programming languages and HTTP is commonly used protocol that is used to transfer data between client and server.

Most effective reason behind the evaluation of Web Services is its platform and language independent nature, because of using xml as a communicative languages web services enables the developer to expose application functionality across the world without being worried about language or platform changes.

Web Services can be exposed in different platforms, most commonly used platform components are listed below.
1) SOAP
2) UDDI
3) WSDL


What is SOAP ?

SOAP stands for Simple Object Access Protocol, it is a xml based protocol that enables the applications to communicate over HTTP. SOAP is a w3c standard, platform and language independent format for sending and receiving messages. To know more about SOAP check out SOAP Documentation.


What is UDDI ?

UDDI stands for “Universal Description, Discovery and Integration”, where business can be registered and searched for web services. UDDI is an platform independent framework for describing, discovering and integrating businesses using internet. UUID is itself a Web Service that is built in MS .net, it is a directory for web services interfaces that are described by WSDL and implemented in SOAP. To know more about UDDI check out UUID Documentation.


What is WSDL ?

WSDL is an acronym for ‘Web Services Description Language’. WSDL is a simple xml document that describes a web service and specifies the location and operations or methods that a web service exposes. To know more about WSDL check out WSDL Documentation.


Web Services in Spring

Any application can have a Web Service component and these components can be created regardless of any language or platform. In this series of blogs we will cover all useful aspects related to Web Services in Spring.

When it comes to developing a Web Service there are two main approaches of doing this,
1)      Contract First Web Services
2)      Contract Last Web Services


Difference between Contract First and Contract Last Web Services

In Contract first Web Services the Contract (WSDL), XML’s and Endpoints are developed first before writing any Service code. While in Contract Last Web Services existing application logic in implemented as a Service and Contract is prepared at the very end.

Writing Contract First Web Services in Spring

Contract First Web Services is the most preferable approach in Spring to develop Web Services. In this approach we starts with an XML file so called WSDL/Contract and Java code is written accordingly. A Contract is an XML file that describes the format request and response, endpoint location and other useful things in it.

There are two types of contracts in Spring Web Services
1)      Data Contracts
2)      Service Contracts


Data Contract

Data Contracts are xml  files that defines the message formats we accept, there are many approaches to define data contracts like DTD’s, RELAX NG, Schematron and XSD the first three are not so popular and have their drawbacks as well. XSD so called XML Schemas are most commonly used approach to prepare a data contract. The easiest way to create a XSD is create an existing XSD file and updating it according to your needs.. These files are saved as .xsd files, here is an example of how XSD looks like
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://beingjavaguys.spring.com"
      targetNamespace=" http://beingjavaguys.spring.com "
      elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:element name="User" type="User" />
      <xs:complexType name="User">
            <xs:sequence>
                  <xs:element name="UserId" type="xs:string" />
                  <xs:element name="UserName" type="xs:string" />
                  <xs:element name="UserGender" type="xs:string" />
                  <xs:element name="UserCity" type="xs:string" />
            </xs:sequence>
      </xs:complexType>
</xs:schema>  
       

Service Contract

Service Contracts are represented by WSDL file, in Spring there is no need for writing an WSDL file manually this can be done programmatically from XSD files. A Service contract or WSDL looks something like this :
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch0="http://com/javaguys/accountservice" xmlns:sch1="http://com/javaguys/accountservice" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://com/javaguys/accountservice" targetNamespace="http://com/javaguys/accountservice">

<wsdl:types>

<xsd:schema xmlns="http://com/javaguys/accountservice" xmlns:user="http://com/javaguys/accountservice" xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://com/javaguys/accountservice">

<xsd:import namespace="http://com/javaguys/accountservice"/>
<xsd:element name="UserDetailsRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="UserId" type="xsd:string"/>
<xsd:element name="UserName" type="xsd:string"/>
<xsd:element name="UserGender" type="xsd:string"/>
<xsd:element name="UserCity" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="UserDetailsResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="UserId" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xs:schema xmlns="http://com/javaguys/accountservice" xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://com/javaguys/accountservice">
<xs:element name="User" type="User"/>
<xs:complexType name="User">
<xs:sequence>
<xs:element name="UserId" type="xs:string"/>
<xs:element name="UserName" type="xs:string"/>
<xs:element name="UserGender" type="xs:string"/>
<xs:element name="UserCity" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="UserDetailsRequest">
<wsdl:part element="tns:UserDetailsRequest" name="UserDetailsRequest"></wsdl:part>
</wsdl:message>
<wsdl:message name="UserDetailsResponse">
<wsdl:part element="tns:UserDetailsResponse" name="UserDetailsResponse"></wsdl:part>
</wsdl:message>
<wsdl:portType name="AccountDetailsService">
<wsdl:operation name="UserDetails">
<wsdl:input message="tns:UserDetailsRequest" name="UserDetailsRequest"></wsdl:input>
<wsdl:output message="tns:UserDetailsResponse" name="UserDetailsResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AccountDetailsServiceSoap11" type="tns:AccountDetailsService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="UserDetails">
<soap:operation soapAction=""/>
<wsdl:input name="UserDetailsRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="UserDetailsResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AccountDetailsServices">
<wsdl:port binding="tns:AccountDetailsServiceSoap11" name="AccountDetailsServiceSoap11">
<soap:address location="http://localhost:8080/Server-FormSubmit/endpoints"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
        
We will cover the components and tags of this WSDL in our Next Tutorial.

Therefore, to create an Web Service in Spring we need an XSD file, WSDL can be achieved from it, and a Endpoint that will implement and listen incoming xml messages.

In this blog we saw, what are Web Services ? their importance in web development and a little about Spring Web Services. In our next tutorials we will try to create a Web Service API using Spring and a Client for that Web Service.









Thanks for reading !
Being Java Guys Team




2 comments:

  1. Nice tutorial....You can follow the basic of Webservice here:

    http://ayazroomy-java.blogspot.in

    ReplyDelete
  2. i review all this it was nice! check this one web development services thanks for sharing..

    ReplyDelete

Like Us on Facebook


Like Us On Google+



Contact

Email: neel4soft@gmail.com
Skype: neel4soft