Web Service Lectures
Web Service Lectures
1/12/2025
Service-Oriented Architecture (SOA)
SOA is a software development approach that emphasizes the creation of reusable and
interoperable software services.
Service-Oriented
Architecture (SOA)
Service-Oriented)
1/12/2025
In this architectural style, business functions and processes are implemented as software
services,
Service-Oriented
Architecture (SOA)
Service-Oriented)
1/12/2025
Common applications of SOA include the following
1/12/2025
The SOAP web services architecture is based on interactions between three components:
a service provider, a service requester, and an optional service registry.
The service provider
• The collection of software that provides a web service.
Service-Oriented • The application program
Architecture (SOA) • The middleware
• The platform on which they run
The service requester
• The collection of software that is responsible for requesting a web service from a
service provider.
• The application program
• The middleware
• The platform on which they run
The service registry
The service registry is a central location where service providers can publish their service
descriptions and where service requesters can find those service descriptions.
1/12/2025
What is SOAP
SOAP stands for Simple Object Access Protocol, it's a transport protocol based on
XML and HTTP. It sends and receives requests and responses.
What is JaxWS
JaxWS stands for Java API for XML Web Services. It builds web services and clients
that communicate using XML.
What is SoapUI
SoapUI is an API Testing Tool, it is used for RESTful Web Services or HTTP based
services as well as SOAP Web Services
1/12/2025
1) Identify Business Services
Define Business Functions: Identify core functions and processes that can be represented
as services (e.g., Customer Management, Order Processing).
1/12/2025
1. Build
development and testing of the Web service
implementation, the definition of the service interface description and the definition of the
service implementation description.
2. Deploy
development includes the publication of the service interface and service
lifecycle implementation definition to a service requestor or service registry and deployment of the
executables for the Web service into an execution environment.
3. Run
the Web service is available for invocation. At this point, the Web
service is fully deployed, operational and network-accessible from the service provider.
4. Manage
ongoing management and administration of the Web service
application. Security, availability, performance and business processes
1/12/2025
API and Web Service
1.Similarities:
• Both facilitate communication between software applications.
• Both use protocols like HTTP.
• Both can enable integration and automation.
2.Differences:
• API (Application Programming Interface) is a broader term that includes Web Services but
also encompasses other types of interfaces, such as libraries or system APIs.
• Web Services specifically refer to APIs that use web protocols for communication.
APIs and Web Services are basic in modern development for creating microservices, integrating
third-party functionalities, and enabling cloud-based systems.
APIs and Web Services are fundamental to software interoperability, enabling applications to
share data, perform actions, and build connected ecosystems.
Java provides a set of APIs (Application Programming Interfaces) for building
web services.
• AX-WS Application: JAX-WS (Java API for XML Web Services) : allows developers to
Java Web Services create SOAP-based web services.
API • JAX-RS Application: JAX-RS (Java API for RESTful Web Services) : with a set of
annotations and classes to create RESTful endpoints using Java.
1/12/2025
JAX-WS, which allow developers to mark Java classes and methods for easy
deployment as web services.
For instance:
JAX-WS • @WebService: Defines the class as a web service.
• @WebMethod: Marks a method as accessible via the web service.
• @WebParam: Defines parameters for the web service methods.
WSDL Generation: JAX-WS can automatically generate a WSDL (Web
Services Description Language) document, which describes the web
service's operations, data types, and endpoint information. WSDL is
essential for clients as it enables them to understand how to interact with
the service.
Data Binding with JAXB: JAX-WS uses JAXB (Java Architecture for XML
Binding) to convert between Java objects and XML, allowing for seamless
XML handling within Java code.
1/12/2025
Task 1
Financial Transactions
In the financial domain, where sensitive data exchange and compliance are paramount, these
technologies are instrumental in defining and securing web services.
Telecommunications Networking
In telecommunications, SOAP and WSDL facilitate structured and interoperable communication
protocols across various network components.
WSDL Files with Apidog
XML Essentials
Introduction to XML
XML (eXtensible Markup Language) is a markup language designed to store and transport
data in a platform-independent and human-readable format.
It uses tags to structure information and is both machine-readable and human-readable.
Common applications include configuration files, data exchange between systems, and web
services.
XML Tree Structure
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
<!DOCTYPE book [
DTD vs. XSD <!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
DTD (Document Type Definition) ]>
<book>
<title>Learning XML</title>
Simple way to define the structure of an XML document. <author>Jane Doe</author>
<price>39.99</price>
</book>
Limited in functionality and lacks support for data types.
Example XSD:
xml
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="book">
<xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element
name="author" type="xs:string"/> <xs:element name="price" type="xs:decimal"/>
</xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Validation:
•XML documents can be validated against XSDs to ensure compliance.
•Tools like xmllint, online validators, or IDE plugins assist in validation.
XML Parsing?
The process of reading and processing an XML document to extract or manipulate data.
Types of parsers:
DOM (Document Object Model): Loads the entire XML document into memory as a tree
structure.
SAX (Simple API for XML): Reads XML sequentially without loading the entire document
into memory.
StAX (Streaming API for XML): Combines the advantages of DOM and SAX by allowing
both sequential reading and event-based processing.
Various Other XML Binding APIs
JAXB (Java Architecture for XML Binding): Maps XML to Java objects and vice versa.
NET XML Serialization: In the .NET framework, used to serialize objects to XML and
Key Components:
1.Envelope:
1. The root element of a SOAP message.
2. Defines the namespace and structure of the message.
2.Header (Optional):
1. Contains metadata or processing instructions.
2. Used for authentication, transaction management, or routing.
Body:
•The main content of the message.
•Contains the actual payload, such as the request or response data.
•Errors and faults are also included here.
<soap:Header>
<auth:Security xmlns:auth="http://example.com/security">
<auth:Token>ABC123</auth:Token>
</auth:Security>
</soap:Header>
<soap:Body>
<m:GetStockPrice xmlns:m="http://example.com/stock">
<m:StockSymbol>GOOGL</m:StockSymbol>
</m:GetStockPrice>
</soap:Body>
Fault (Optional):
•Communicates errors encountered during processing.
•Contains elements like faultcode, faultstring, and detail.
Role of SOAP in Web Services
SOAP plays a crucial role in enabling communication between applications over the internet. It is widely used
in web services for the following reasons:
Key Roles of SOAP:
1.Platform-Independent Communication:
1. Uses XML, ensuring interoperability across different programming languages and platforms.
2.Transport Protocol Flexibility:
1. Works over HTTP, HTTPS, SMTP, or other protocols.
3.Standardized Messaging Framework:
1. Provides a structured format for request and response messages.
4.Support for Complex Operations:
1. Handles multiple operations, return types, and error reporting.
5.Security and Reliability:
1. Integrates with WS-Security for message-level encryption and authentication.
2. Can be used with protocols like WS-ReliableMessaging for guaranteed delivery.
6.Error Handling:
1. Standardized fault reporting mechanism simplifies debugging and error communication.
Implementing SOAP Web Services
• There are certain steps to implement SOAP Web Services with JAX-WS
• First, you need to define Service endpoint interfaces (SEI) which specify the methods
to expose as web service.
• Next, you need to implement SEI with a Java class.
• Then you need to Annotate the SEI and its implementation class with JAX-WX
annotations to specify the web service details.
• Package web service classes to the WAR file and deploy it to a web server.
2. Implementing RESTful Web Services with JAX-RS
• First you need to define the resources that represents the web services and its
methods.
• Then you need to annotate the resource class and its methods with JAX-RS
annotations to specify the web service package.
• At last we need to package the web service classes and deploy it to a web server.
Implementing Code–First Web Services
• But this approach provides less control over the service contract as
the WSDL is generated by WSO2 Web Services Application Server
(WSO2 WSAS). If someone changes the service code, WSDL will be
changed accordingly.
Contract First Approach
• development of the service is started from a WSDL definition, which
becomes a contract between the service provider and the client.
• In this approach, WSDL definition is defined using Web services standards
and the service implementation is done after that according to this
contract.
Holiday
In the scenario, we have to deal with holiday requests, so it makes sense to determine what a holiday looks
like in XML:
<Holiday xmlns="http://mycompany.com/hr/schemas">
<StartDate>2006-07-03</StartDate>
<EndDate>2006-07-07</EndDate>
</Holiday>
A holiday consists of a start date and an end date.
Employee What is important is that all of the data is there. In fact, the
There is also the notion of an employee in the scenario. data is the only thing that is important: we are taking a data-
Here is what it looks like in XML: driven approach.
<Employee
xmlns="http://mycompany.com/hr/schemas">
<Number>42</Number>
<FirstName>Arjen</FirstName>
<LastName>Poutsma</LastName> </Employee>
HolidayRequest
Both the holiday and employee element can be put in a <HolidayRequest/>:
<HolidayRequest xmlns="http://mycompany.com/hr/schemas">
<Holiday>
<StartDate>2006-07-03</StartDate>
<EndDate>2006-07-07</EndDate>
</Holiday>
<Employee>
<Number>42</Number>
<FirstName>Arjen</FirstName>
<LastName>Poutsma</LastName>
</Employee>
</HolidayRequest>
Data Contract
This data contract defines the message format we accept. There are four different ways of defining
such a contract for XML:
•DTDs
•XML Schema (XSD)
•RELAX NG
•Schematron
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. We will use XML Schema.
• Any good XML editor or Java IDE offers easiest way to create an XSD
1. all tells the XML parser that the order of <Holiday/> and <Employee/> is not significant.
2. We use the xsd:date data type, which consist of a year, month, and day, for <StartDate/> and <EndDate/>.
3. xsd:string is used for the first and last name.
Service contract
• A service contract is generally expressed as a WSDL file. Note that in Spring-WS, writing
the WSDL by hand is not required. Based on the XSD and some conventions, Spring-WS
can create the WSDL for you.
1. We import the schema “Data Contract”.
2. We define the HolidayRequest message, which gets used in the portType.
3. The HolidayRequest type is defined in the schema.
4. We define the HumanResource port type, which gets used in the binding.
5. We define the HumanResourceBinding binding, which gets used in the port.
6. We use a document/literal style.
7. The literal http://schemas.xmlsoap.org/soap/http signifies a HTTP transport.
8. The soapAction attribute signifies the SOAPAction HTTP header that will be sent with every request.
9. The http://localhost:8080/holidayService/ address is the URL where the Web service can be invoked.
Implementing the Endpoint
In Spring-WS, you will implement Endpoints to handle incoming XML
messages. An endpoint is typically created by annotating a class with
the @Endpoint annotation. In this endpoint class, you will create one
or more methods that handle incoming request. The method
signatures can be quite flexible: you can include just about any sort of
parameter type related to the incoming XML message.
• import org.springframework.beans.factory.annotation.Autowired;
• import org.springframework.ws.server.endpoint.annotation.Endpoint;
• import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
• import org.springframework.ws.server.endpoint.annotation.RequestPayload;
1. The HolidayEndpoint is annotated with @Endpoint.
This marks the class as a special sort of Component, suitable for handling XML messages in Spring-WS, and also making it eligible
for suitable for component scanning.
2. The HolidayEndpoint requires the HumanResourceService business service to operate, so we inject the dependency via the
constructor and annotate it with @Autowired. Next, we set up XPath expressions using the JDOM API. There are three
expressions: //hr:StartDate for extracting the <l;StartDate> text value, //hr:EndDate for extracting the end date and
concat(//hr:FirstName,’ ‘,//hr:LastName) for extracting and concatenating the names of the employee.
3. The @PayloadRoot annotation tells Spring-WS that the handleHolidayRequest method is suitable for handling XML messages.
The sort of message that this method can handle is indicated by the annotation values, in this case, it can handle XML elements
that have the HolidayRequest local part and the http://mycompany.com/hr/schemas namespace.
• The handleHolidayRequest(..) method is the main handling method method, which gets passed with the <HolidayRequest/>
element from the incoming XML message.
• The @RequestPayload annotation indicates that the holidayRequest parameter should be mapped to the payload of the
request message. We use the XPath expressions to extract the string values from the XML messages, and convert these
values to Date objects using a SimpleDateFormat. With these values, we invoke a method on the business service. Typically,
this will result in a database transaction being started, and some records being altered in the database.
• Finally, we define a void return type, which indicates to Spring-WS that we do not want to send a response message. If we
wanted a response message, we could have returned a JDOM Element that represents the payload of the response message.
Routing the Message to the Endpoint
As part of writing the endpoint, we also used the @PayloadRoot annotation to indicate which sort
of messages can be handled by the handleHolidayRequest method. In Spring-WS, this process is
the responsibility of an EndpointMapping. Here we route messages based on their content, by
using a PayloadRootAnnotationMethodEndpointMapping. The annotation used above:
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data format for structuring and exchanging
data. It is commonly used with AJAX for server responses and requests.
How AJAX and JSON Work Together
AJAX is a very popular concept that is used to update the page without reloading the page.
AJAX stands for Asynchronous Javascript And XML and because of that many Developers
think that AJAX will only use XML to export and import data but that is not true. AJAX can use
XML to transport any kind of data but can also transport data in JSON or any other plain text.
When interacting with a RESTful API, the client typically sends HTTP requests to the server. The
request may contain JSON data, especially when creating or updating resources (using POST or
PUT).
Here, the Content-Type: application/json header tells the server that the body of the request contains JSON
data. The server parses the JSON and processes it to create a new resource.
Securing Web service
The general approach
■ Transport-level security such as firewalls, virtual private networks, basic
authentication, non-repudiation, and encryption.
■ Message-level security such as using authentication tokens to validate
requester identity and authorization assertions to control access to
provider services.
■ Data-level security such as encryption and digital signature to protect
against altering stored and/or transmitted data.
■ Environment-level security such as management, logging, and auditing to
identify problems that need to be fixed and establishing trusted relationships and
communication patterns.
• Identity: Identity management for Web services is similar to identity
management for any IT system in that the subject (whether a person, machine,
program, or abstraction such as a process flow) is given a unique or unambiguous
name within the security domain