How to create a Data Contract for Spring Web Service

Before we create a data contract for a Spring Web Service lets try to understand how it fits the big picture.
So as per the definition from Wikipedia,
“A Web service is a method of communication between two electronic devices over a network.” So this two electronic devices need to understand the language of each other to communicate, hence we need a common  language. The server which provides  the service to the client establishes a contract based on which this service is rendered to the clients.
a) WSDL or Web Services Description Language  is the service contract which forms the basis on which the web-service client and the web-service server exchange data.
b) XML Schema Definition (XSD) describes the structure of the data which will flow between the web-service server and the web-service client.A data contract is the vital part of Spring-Web-Service and a service contract can be generated from a data contract.
c) To write contract-first Web services, that is, developing web services that start with the XML Schema/WSDL contract first followed by the Java code second we need a contract.
There are four different ways of defining such a contract for XML:
  • DTDs
  • XML Schema (XSD)
  • RELAX NG
  • Schematron
Explanation from Springs Website:
DTDs have limited namespace support, so they are not suitable for Web services. Relax NG and Schematron certainly are easier than XML Schema. Unfortunately, they are not so widely supported across platforms.”
So let use this xml as sample, and create the XSD:
employee.xml
employee.xml
There are various plugins for eclipse, netbeans ,websites and utility tools like xmlbeans which can take a xml and create a XSD. The XSD will look like below:
Employee XSD
Employee XSD
The data contract is self explanatory, this is the starting point of creating any web service.

Comments