Web services are services exposed over a network and is designed to be used automatically by other applications without human interaction.
A practical example for web services is a news web site which also shows cricket score. The cricket score service can be provided as a web service on a different server and our news web site can automatically invoke that service without any human intervention, get the result and update it along with the news. Note that this can also be done using Ajax.
Web services are different from web applications. While web applications are applications with web pages that reside on a server, primarily for use by humans, a web service is a server-based application component meant primarily for interaction with other programs. A web application might use a web service to retrieve and populate some data from another server without the end user knowing about it.
WebServices can be roughly divided into two types based on the architecture they follow:
SOAP-based web services
Restful web services
SOAP-based web services were the original kind of web services
They follow RPC-style
An RPC-style web service accepts an envelope full of data from its client, and sends a similar envelope back.
The method and the scoping information are kept inside the envelope. HTTP is a popular envelope format.
SOAP is another popular envelope format (transmitting a SOAP document over HTTP puts the SOAP envelope inside an HTTP envelope).
SOAP Services can be located and invoked by client applications over a network (e.g. internet) automatically without human interaction.
To make the automatic locating and invocation possible SOAP web services were designed as self describing through the use of a message protocol called SOAP, service definition called WSDL and a way to locate other web services using UDDI.
SOAP web services are self-describing and self-contained, as the definition of the message format (WSDL and/or XSD) travels with the message or is accessible to the client, and the server and client requires only minimal software - an HTTP server and/or a SOAP server on the server side and on the client side no additional s/w is required.
SOAP is based on rules whereas REST is based on guidelines.
SOAP web service defines a new vocabularies, standards and protocols.
In SOAP based webservices messages (requests and responses) are exchanged in an XML format called SOAP.
SOAP originally stood for Simple Object Access Protocol, but now some also call it as Service Oriented Architecture (SOA) protocol.
Though HTTP is the most commonly used protocol with SOAP-based webservices, it is not limited to just HTTP, but can also use other protocols like SMTP, JMS etc.
A SOAP-based service delivered over HTTP can be considered as a special case of a REST-style service.
In SOAP, the endpoint url will be same for a service that represent a set of operations and different operations are requested using specifics in the request body.
Definition about the service is stored in a document called WSDL in case of SOAP. When a client get a wsdl, it will have all information regarding a SOAP web service.
The request-response pattern is supported by both SOAP and REST. SOAP based services also support other message exchange patterns like one way input, solicit response and publish-subscribe.
You need a specialized client that can send SOAP requests in case of SOAP.
SOAP UI is a popular tool that can do this.
You may also write a Java client using JAX-WS on your own.
Restful web services came later and is more popular now
They follow a resource oriented way. Every resource will be reprecented by an URI and its current state of representation is transferred using formats such as XML and JSON.
REST web services are not automatically located using an UDDI registry as in case of SOAP web services.
SOAP is based on rules whereas REST is based on guidelines
Restful web services share a standard vocabulary of HTTP methods.
Every object in a Restful service responds to the same basic interface.
In a REST-style service, a client might send a standard HTTP request to a web service and receives an appropriate response in a MIME-TYPE of their choice like XML, Text, JSON, user-defined etc.
HTTP is the only transport protocol for REST.
REST usually have a single url for a resource and uses the standard set of http operations like POST, GET, PUT, and DELETE, to do various operations on that resource.
Note that REST is based on guidelines and not rules as in the case of SOAP. So there is nothing which will enforce you to use a particular approach in REST, like in the case of SOAP.
There is no formal definitions for REST like the WSDL for SOAP. There are some definition formats like WADL, but they have not become very popular.
REST support only request-response message exchange pattern.
You can make requests to a RESTful web service using your programming language’s HTTP client library or even from your web browser if it is a GET request.
There are also plugins available to most browsers.
Java supports both SOAP-based and REST-style web services.
Java API for XML Web services (JAX-WS) can be used for SOAP-based as well as REST-style, but more suited for SOAP.
The current version of JAVA–WS is 2.x and previous version was called JAX RPC 1.x.
JAX–WS is commonly shortened to JWS for Java Web Services.
JAX–WS was initially seperate, but was then bundled into the Metro web services pack.
Metro is part of java 6 and above and hence JAX-WS web services can be compiled and deployed in java 6 and above without any additional software.
The downloaded Metro release is a way to do JAX–WS under core java 5.
You can download metro from metro.java.net and just JAX-WS from jax-ws.java.net.
JAX-WS alone supports only SSL (it supports WS-I BSP).
Hence if you want end-to-end message-level security then you need the WSIT/Tango layer of Metro.
In addition to JAX-WS RI, metro contains another layer, WSIT/Tango which provides WS-*: Security, SecureConversation, Trust, ReliableMessaging, AtomicTransactions and MEX.
JAX–WS require at least core java 5 because of the support of annotation.
Java API for XML – Restful web services (JAX–RS) is the standard specification for REST web services.
Jersy is a popular JAX-RS implementation.
Java defines standard REST support via JAX-RS (The Java API for RESTful Web Services) in JSR 311.
You can download JAX-RS from Jersy Homepage at jersey.java.net.
Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services.
In addition to the Reference Implementation, Jersey also provides an API so that developers may extend Jersey to suit their needs.
Jersy is only one of the implementations for it. There are also other implementations like RESTEasy which may also be used.
Since all these implementations are based on the JAX-RS API, you can easily switch to another implementation anytime.
Message Exchange Patterns (MEP) is a pattern for the exchange of messages between two communicating parties like a server and a client. The most common such pattern is the request-response pattern where the client sends a request and the server sends a response back. For more details on MEPs read message-exchange-patterns-in-web-services.