Unit 4
Unit 4
Web Services
Spring Web Services (Spring-WS) is a product of the Spring community and is focused on
creating document-driven web services. Spring Web Services aims to facilitate contract-first
SOAP service development, allowing for the creation of flexible web services by using one of
the many ways to manipulate XML payloads. The product is based on Spring itself, which
means you can use the Spring concepts (such as dependency injection) as an integral part of
your web service.
1. Powerful mappings
You can distribute incoming XML requests to any object, depending on message payload,
SOAP Action header, or an XPath expression.
2. XML API support
Incoming XML messages can be handled not only with standard JAXP APIs such as DOM,
SAX, and StAX, but also with JDOM, dom4j, XOM, or even marshalling technologies.
3. Flexible XML Marshalling
Spring Web Services builds on the Object/XML Mapping module in the Spring Framework,
which supports JAXB 1 and 2, Castor, XMLBeans, JiBX, and XStream.
4. Reusing Your Spring expertise
Spring-WS uses Spring application contexts for all configuration, which should help Spring
developers get up-to-speed quickly. Also, the architecture of Spring-WS resembles that of
Spring-MVC.
5. Support for WS-Security
WS-Security lets you sign SOAP messages, encrypt and decrypt them, or authenticate against
them.
6. Integration with Spring Security
The WS-Security implementation of Spring Web Services provides integration with Spring
Security. This means you can use your existing Spring Security configuration for your SOAP
service as well.
7. Apache license
You can confidently use Spring-WS in your project.
Runtime environment
Spring Web Services requires a standard Java 8 Runtime Environment. Spring-WS is built on
Spring Framework 4.0.9, but higher versions are supported.
Spring-WS consists of a number of modules, which are described in the remainder of this
section.
The XML module (spring-xml.jar) contains various XML support classes for Spring
Web Services. This module is mainly intended for the Spring-WS framework itself and
not web service developers.
The Core module (spring-ws-core.jar) is the central part of the Spring’s web services
functionality. It provides the central WebServiceMessage and SoapMessage interfaces,
the server-side framework (with powerful message dispatching), the various support
classes for implementing web service endpoints, and the client-
side WebServiceTemplate.
The Support module (spring-ws-support.jar) contains additional transports (JMS,
Email, and others).
The Security package (spring-ws-security.jar) provides a WS-Security implementation
that integrates with the core web service package. It lets you sign, decrypt and encrypt,
and add principal tokens to SOAP messages. Additionally, it lets you use your existing
Spring Security security implementation for authentication and authorization.
The following figure shows and the dependencies between the Spring-WS modules. Arrows
indicate dependencies (that is, Spring-WS Core depends on Spring-XML and the OXM
module found in Spring 3 and higher).
Supported standards
Spring Web Services supports the following standards:
SOAP 1.1 and 1.2
WSDL 1.1 and 2.0 (XSD-based generation is supported only for WSDL 1.1)
WS-I Basic Profile 1.0, 1.1, 1.2, and 2.0
WS-Addressing 1.0 and the August 2004 draft
SOAP Message Security 1.1, Username Token Profile 1.1, X.509 Certificate Token
Profile 1.1, SAML Token Profile 1.1, Kerberos Token Profile 1.1, Basic Security
Profile 1.1
Service-Oriented Architecture
Service-Oriented Architecture (SOA) is a stage in the evolution of application development
and/or integration. It defines a way to make software components reusable using the
interfaces.
Formally, SOA is an architectural approach in which applications make use of services
available in the network. In this architecture, services are provided to form applications,
through a network call over the internet. It uses common communication standards to speed
up and streamline the service integrations in applications. Each service in SOA is a complete
business function in itself. The services are published in such a way that it makes it easy for
the developers to assemble their apps using those services. Note that SOA is different from
microservice architecture.
SOA allows users to combine a large number of facilities from existing services to
form applications.
SOA encompasses a set of design principles that structure system development
and provide means for integrating components into a coherent and decentralized
system.
SOA-based computing packages functionalities into a set of interoperable
services, which can be integrated into different software systems belonging to
separate business domains.
Services might aggregate information and data retrieved from other services or create
workflows of services to satisfy the request of a given service consumer. This practice is
known as service orchestration Another important interaction pattern is service choreography,
which is the coordinated interaction of services without a single point of control.
Components of SOA:
Guiding Principles of SOA:
1. Standardized service contract: Specified through one or more service
description documents.
2. Loose coupling: Services are designed as self-contained components, maintain
relationships that minimize dependencies on other services.
3. Abstraction: A service is completely defined by service contracts and description
documents. They hide their logic, which is encapsulated within their
implementation.
4. Reusability: Designed as components, services can be reused more effectively,
thus reducing development time and the associated costs.
5. Autonomy: Services have control over the logic they encapsulate and, from a
service consumer point of view, there is no need to know about their
implementation.
6. Discoverability: Services are defined by description documents that constitute
supplemental metadata through which they can be effectively discovered. Service
discovery provides an effective means for utilizing third-party resources.
7. Composability: Using services as building blocks, sophisticated and complex
operations can be implemented. Service orchestration and choreography provide a
solid support for composing services and achieving business goals.
Advantages of SOA:
Service reusability: In SOA, applications are made from existing services. Thus,
services can be reused to make many applications.
Easy maintenance: As services are independent of each other they can be
updated and modified easily without affecting other services.
Platform independent: SOA allows making a complex application by combining
services picked from different sources, independent of the platform.
Availability: SOA facilities are easily available to anyone on request.
Reliability: SOA applications are more reliable because it is easy to debug small
services rather than huge codes
Scalability: Services can run on different servers within an environment, this
increases scalability
Disadvantages of SOA:
High overhead: A validation of input parameters of services is done whenever
services interact this decreases performance as it increases load and response time.
High investment: A huge initial investment is required for SOA.
Complex service management: When services interact they exchange messages
to tasks. the number of messages may go in millions. It becomes a cumbersome
task to handle a large number of messages.
Web Services:
Web Service is the set of rules or guidelines which enable communication among different
applications via the World wide web (.i.e. the internet). Before web service, there were other
technologies but some of them have dependencies such as EJB (enterprise java bean) which
allows applications to communicate only if the applications are working on Java, these
dependencies make communication difficult. These dependencies are removed by web
services.
In the present world, applications are developed on a variety of programming languages such
as Java, Python, PHP, etc. These heterogeneous applications need communication to happen
between them. Since they are developed in different programming languages it becomes
difficult to ensure efficient communication between them. Here is where web services come
into the picture, web services provide a language-independent way of communication that
means the applications working on Java can communicate with other applications working on
Python. Therefore, web service helps us to invoke the functionality of other programs in the
existing program.
Web services use the request-response method to communicate among applications. For any
communication, we need a medium and a common format that can be understood by
everyone, in the case of web services medium is the internet and the common format is the
XML (Extensible Markup Language) format as every programming language can understand
the XML markup language.
A client is the one that requests some service from the server that is known as the service
provider. The request is sent through a message which is in common XML format and in
response to that request, the service provider will respond with a message in a common
format (.i.e. XML).
1. SOAP web services: SOAP stands for Simple Object Access Protocol. These protocols are
based on XML which is a lightweight data exchange language. These protocols are
independent of language and can be run on any platform.
SOAP supports both stateful and stateless operations. Stateful means that the server keeps
track of the information received from the client on each request. While Stateless means that
each request contains enough information about the state of the client and thus server does
not need to bother about saving the state of the client thus increasing the speed of
communication.
Many companies such as IBM, Microsoft are producing an implementation of SOAP into
their systems.
2. RESTful web services: It stands for Representational State Transfer. They are also
language and platform-independent and are faster in comparison to SOAP. Nowadays
RESTful web services are more used than SOAP. They treat the data as resources. RESTful
web services return data in JSON format or XML format. These web services create the
object and send the state of the object in response to the client’s requests, that’s why known
as Representational State Transfer.
SOAP
SOAP is a messaging protocol for exchanging information between two computers based on
XML over the internet. SOAP messages are purely written in XML which is why they are
platform and language independent.
Working with SOAP requests and responses could get very complex. Some languages make
efficient use of the SOAP shortcuts to reduce the level of complexity and the .Net platform
for example hides the XML to a large extent. This is made possible due to the Web Service
Definition Language (WSDL). WSDL is an XML file that defines and describes the services
that are available in the web service of interest. It describes the naming of services, the
specifications and structure of the response sent back. The services in the WSDL are
described as a compilation of network ports/endpoints. With the WSDL, the .Net platform is
able to auto-generate the proxy classes and functions which can be called from the
application.
The types describe the datatypes the web service use.
The message defines each operation’s data element
The portType describes what operations can be done and the messages involved in
this operation. This could be one of four types: One way (receives messages but no
response), Request-Response (receives request and responds), Solicit-Response
(sends request and waits for response) or Notification (sends request, does not require
response
Binding tells the format of data for the type of each port and the protocol.
SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for accessing
web services.
Language and Platform independent: SOAP web services can be written in any
programming language and executed in any platform.
REST
REST is a web standard architecture that achieves data communication using a standard
interface such as HTTP or other transfer protocols that use standard Uniform Resource
Identifier (URI). The design is such that each component in a RESTful web service is a
resource that can be accessed using standard HTTP methods (if the chosen protocol is
HTTP). Resources which can be thought of as objects in the concept of Object oriented
programming (OOP) are identified by URIs and the resources are represented in several ways
such as JSON, XML, Text etc. though JSON is currently the more favoured choice.
RESTful services have the following properties: Representations, Messages, URIs, Uniform
interface, Stateless, Links between resources and caching. A quick look into these properties
below using HTTP
The Response Header and Body hold similar information to the request, only that the
information is different as it is the server response
3. URIs – Each resource needs at least one URI to identify a resource(s) and the
operation is determined by the HTTP verb/action. Hence a URI can be called with
different actions.
4. Stateless- Restful web services are stateless and any session state is held on the client
not server. This ensures that every client to server request has the necessary
information to understand the request and handles each request independently.
5. Links Between Resources- the representation of a resource can have links to other
resources.
6. Caching- the data produced when a request is made the first time is stored and used
the next time in order to stop regenerating same information for the same request and
improves performance. The HTTP headers help to control caching such as age which
tracks how long ago the data was fetched from the server, expires date and time the
resource representation expires etc.
The above gives an oversight of the six properties of REST and it is important to remember
that:
REST is not coupled to HTTP and is actually protocol independent. It is simply not a
mapping of CRUD to the HTTP methods.
REST makes it relatively easy to integrate with websites and are exposed using XML
(one of many ways) for easy consumption
Fast: RESTful Web Services are fast because there is no strict specification like SOAP. It
consumes less bandwidth and resource.
Language and Platform independent: RESTful web services can be written in any
programming language and executed in any platform.
Can use SOAP: RESTful web services can use SOAP web services as the implementation.
Permits different data format: RESTful web service permits different data format such as
Plain Text, HTML, XML and JSON.
The choice on which to use is totally dependent on what the requirement. For example,
SOAP is a better choice for applications that have complex API so as to describe the services
and methods, where formal contracts are agreed for the exchange format, where a guaranteed
level of security is required etc. REST will be preferred when limiting bandwidth and
resources, when operations are can be stateless and the information can be cached.
REST stands for REpresentational State Transfer. REST is web standards based architecture
and uses HTTP Protocol. It revolves around resource where every component is a resource
and a resource is accessed by a common interface using HTTP standard methods. REST was
first introduced by Roy Fielding in 2000.
In REST architecture, a REST Server simply provides access to resources and REST client
accesses and modifies the resources. Here each resource is identified by URIs/ global IDs.
REST uses various representation to represent a resource like text, JSON, XML. JSON is the
most popular one.
HTTP methods
Following four HTTP methods are commonly used in REST based architecture.
A web service is a collection of open protocols and standards used for exchanging data
between applications or systems. Software applications written in various programming
languages and running on various platforms can use web services to exchange data over
computer networks like the Internet in a manner similar to inter-process communication on a
single computer. This interoperability (e.g., between Java and Python, or Windows and Linux
applications) is due to the use of open standards.
Web services based on REST Architecture are known as RESTful web services. These
webservices uses HTTP methods to implement the concept of REST architecture. A RESTful
web service usually defines a URI, Uniform Resource Identifier a service, provides resource
representation such as JSON and set of HTTP Methods.
Show details
3 /UserService/getUser/:id GET empty
of a user.