Spring Book – Chapter 16 – Web Services – Spring WS

Why Web Services?

So, why do we need web service anyway? Below are some of the points which summarize as to why you should use web services in your application:

  • Loosely Coupled Applications – Web Services are self-describing software modules which encapsulate disconnected functionality. They are accessible via standard internet communication protocols like XML and SOAP. They can be developed in any technologies (like Java, .NET, PHP, Perl etc.) and any application or other web services can access these services.
  • Standardized Protocol – Web Services uses industry standard protocol for communication. The use of these standard protocols gives the business advantages like wide range of choices, reduction in the cost due to competition and increase in quality.
  • Connecting Different Applications – Web Services allow different applications to talk to each other and share data and services among themselves. They can be used to make the application platform and technology independent.
  • Low Cost of communication: Web Services uses SOAP over HTTP protocol for the communication which means you can use your existing low cost internet for implementing Web Services.
  • Support for Other communication means – Beside SOAP over HTTP, Web Services can also be implemented on other reliable transport mechanisms. For example Web Services can also be implemented using ftp protocol (Web services over FTP).
  • Automatic Discovery: Web Services automatic discovery mechanism helps the business to easily find the available Service Providers. With the help of Web Services your business can expose their own Web Services to others which can generate revenues.
  • Web Services are Self Describing: Web Services are self describing applications, which reduces the software development time helping other business partners to quickly develop application and start doing business. This helps business to save time and money by cutting development time.

Why XML for Writing Web Services?

One of the most important goals of a Web service is to provide interoperability and loose coupling. To support multiple platforms such as Java, .NET, etc. which have different class libraries, you must use some common format to communicate between them. The de-facto format at present available in industry is XML, which is supported by most of the popular languages in all aspects.

Spring Web Service lets you focus on writing these XML schemas while creating your web services.

Why Use a Framework for Web Services?

The advantages of using a web service framework to do the web service development include the following:

  • The framework will do the routing stuff for you in a consistent way
  • The framework will handle the complexity involved with technology like SOAP, which is a very complex in nature.
  • The framework will handle the processing dealing with XML, such as marshaling and un-marshaling, which is must for a web service to function.
  • The reduction in manually written code is directly proportional to the reduced number of bugs
  • Using the framework allows the developer to do the important tasks of developing the business logic rather than worrying about the nitty-gritties involved in creating the actual web service.

Web Services Types

Web services can be classified according to various criteria, but for the purposes of this book, we can define two types of web services:

  • RPC-based – In RPC-based web service you decide as to what operations you want your web services to expose and accordingly use the WSDL to describe those operations and then do the implementation. Your WSDL will have the binding section which contains the various operations or methods which your web services expose to the outside world.
  • Document-based – In the document-based approach you decide on what XML document structure you want your web service to send in as input and what XML document you want the web service to return from your web service as a response. It is not based on operation, as opposed to RPC-based web services.

Spring WS encourages you to design your web service in a more practical document-based style rather than RPC-based. Spring WS makes you think more in terms of WSDL. It pushes you to think more into XSD and then Spring-WS pitches in and can auto-generate the WSDL from the XSD schema.

Web service creation using Spring WS can be broken down into the following two steps:

  1. Create your XML schema (XSD file) and define inside the schema your request and response message structures. For this you can use your favorite schema editor to create the schema or write sample request and response XML and then reverse-engineer the schema if your tool supports this way of doing it.
  2. Use Spring WS to point to the XSD and set up a few Spring managed beans and you have the web service already setup without writing WSDL.

This way of creating web service is called as contract-first approach to building web services, which will be discussed later in this Chapter in more detail.

RESTful Web Services

Another type of web service which has become more and more significant over a period of time is called as RESTful web Service. A RESTful web service is a web service implemented using the HTTP protocol and using the principles of REST. Spring Framework added support for building RESTful web services in Release 3.x. The Spring’s REST support is not an implementation of JAX-RS but it in fact has more features than the specification definition. At the same time, the REST support is integrated seamlessly into Spring’s MVC layer making it easy to be adopted by applications that build with using the Spring Framework.

JAX-RS: Java API for RESTful Web Services is a Java programming language API that provides support in creating web services according to the Representational State Transfer (REST) architectural style.

Representational State Transfer (REST) defines a set of architectural principles coined by Roy Fielding in his doctoral dissertation in the year 2000, by which you can design Web services that focus on a system’s resources, including how resource states are addressed and transferred over the HTTP protocol by a wide range of clients written in different languages.

The REST design principle establishes a one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP protocol methods. The mapping existing between the two can be summarized as below:

  • To create a resource on the server, use POST.
  • To retrieve a resource, use GET.
  • To change the state of a resource or to update it, use PUT.
  • To remove or delete a resource, use DELETE.

The query string in a request Uniform Resource Identifier (URI) includes a set of parameters which can be used by the server to find a set of matching resources.

Designing Web services using REST principles creates less dependence on proprietary middleware like an application server as compared to SOAP and WSDL based web service design.

Similarly in modern day web applications XML over HTTP has become a very powerful mechanism which allows internal applications use Asynchronous JavaScript + XML (Ajax)-based custom user interfaces which easily connects and consumes resources exposed in the internet. In fact the fit between Ajax and REST has increased the amount of attention REST has gained over past some years.

Roy Fielding, is one of the principal authors of the HTTP specification versions 1.0 and 1.1, introduced REST in his doctoral dissertation in 2000.

Page Visitors: 11145

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *