You are on page 1of 5

Web Services

Web Services
http://docs.oracle.com/javaee/6/tutorial/doc/gilik.html http://geronimo.apache.org/GMOx O!"#/developing$ client$%or$rest%ul$&eb$service.html

Outline
I. What is Web Services? II. Why Web Services? III. Web Services Architecture IV. Web Services Description Language (WSDL) V. RESTful Web Services VI. Developing Client for RESTful Web Service
3

'. What is Web Services( )*+


Web Services is a platform to build loosely coupled applications.
A web service is piece of code that can be remotely invoked using HTTP, that means an HTTP request is enough to invoke a web service. Moving back in time, remote access to binary data has been either platform specific or vendor specific.

For example:
Microsoft's DCOM (Distributed Component Object Model) for communication between networked computers access remote COM (Component Object Model) interface through remote procedure calls. Corba uses IIOP (Internet Inter-ORB Protocol) to access remote objects. In Java RMI (Remote Method Invocation) is used to access an EJB (Enterprise Java Bean) object which is again language specific. All of the above examples suggest that remotely accessing an object required proprietary technologies that were tightly coupled to the remote code.
4

What is Web Services( ),+


Web Services on the other hand, is basically a platform to built a distributed architecture where there are numerous computers connected to each other in a network. The various computers in the network will be running applications developed using different tools, different technologies from different vendors. Each of these applications can be exposed as a Web Service using SOAP, UDDI and WSDL wherein a service running on one computer can access a service running on other irrespective of difference in technologies. So, if there is a .Net application running on one computer it can be accessed using a Java application from other, only condition is both should be exposed as a Web service. Web Service clients can be console based as well as browser based.

''. Wh- Web Services(


There are several reasons why Web Services has become a need:
1. One of the biggest reasons is interoperability wherein different vendor specific applications can interact with each other.
For Example a .Net application accessing a Java web service.

2. Using Web Services you can expose your application and its functionality globally. 3. Using Web Services, an enterprise would not not depend on one particular vendor for all the solutions.
It can move to different vendors for different functionality and can optimally choose out of several options.

4. Web Services use standardized protocols SOAP, UDDI, WSDL and HTTP for implementation. 5. Web Services has support for most of the communication protocol and it can be implemented using FTP as well as HTTP. 6. Web Services follow a loosely coupled architecture.
6

'''. Web Services .rchitecture )*+

Web Services .rchitecture ),+


Web Service Provider registers the Web Service to UDDI registry and provides WSDL for invoking service. Web Service consumer, which can be an application client or any other Web Service, queries the UDDI registry and finds WSDL. Consumer uses WSDL to invoke the SOAP service.

'/. Web Services escription 0anguage )WS 0+ )*+


WSDL is an XML based way of describing a Web Service. It specifies the location of Web Service and methods available with the service. A WSDL document has <portType>, <message>, <types> and <binding> as the elements.

Web Services escription 0anguage )WS 0+ ),+


Syntax of a WSDL document <definitions> <types> definition of types.... </types> <message> definition of message.... </message> <portType> definition of portType.... </portType> <binding> definition of binding.... </binding> <service> definition of service.... </service> </definitions>
9

The syntax of a WSDL suggests that it is a set of definitions where the definition element is at the root.
10

Web Services escription 0anguage )WS 0+ )"+


HelloWorld.wsdl
<!--WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006 (06:55:48 PDT)--> <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions> <wsdl:types> <schema elementFormDefault="qualified targetNamespace="http://webservices" xmlns="http://www.w3.org/2001/XMLSchema"> <element name="hello"> <complexType/> </element> <element name="helloResponse"> <complexType/> </element> </schema> </wsdl:types> <wsdl:message name="helloRequest"> <wsdl:part element="intf:hello" name="parameters"/> </wsdl:message>
11

Web Services escription 0anguage )WS 0+ )1+


<types>
Web Service is all about sending and receiving messages. The <types> element describes the various messages which will be used by the service. Basically it defines the various data types. HelloWorld service uses hello and helloResponse as the two messages.

<message>
This element defines the various messages used by the Web service. In an example two message name,
helloRequest which is associated with hello data type and helloResponse which is associated with helloResponse data type as defined in the <types> element.

Each message type has unique name which is suggested by <message name=""> tag. The <part element="" ...> suggests the data type associated with each message. <part> element can be considered as a parameter to a function call.
12

Web Services escription 0anguage )WS 0+ )2+


<portType>
his basically involves a set of operation and messages involved in each operation. <portType name=""> defines a unique name. In the example <wsdl:portType name="HelloWorld"> suggest a unique name. Each portType has an associated <operation name=""> element which suggest an operation name. In the example <wsdl:operation name="hello"> suggests an operation name. Each operation element can have <input>, <output> or both the tags accordingly it will be one-way, notification or request-response operation. The example has
<wsdl:input message="intf:helloRequest" name="helloRequest"/> which uses the helloRequest as the input message and <wsdl:output message="intf:helloResponse" name="helloResponse"/> 13 which uses the helloResponse as the output message.

Web Services escription 0anguage )WS 0+ )6+


<bindings>
A binding defines message format and protocol details for operation and message defined by each portType. Each binding element is identified by a unique name attribute which can be any user defined name. The type element defines the portType referenced by binding. In the case <wsdl:binding name= "HelloWorldSoap Binding" type="intf:HelloWorld"> defines a binding with a unique name and HelloWorld portType associated with it. The soap:binding element has style attribute which can be either RPC-oriented (messages containing parameters and return values) or document-oriented (message containing documents). The default value for this element is document. The other element is transport which suggests the transmission protocol used. In the example, <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> document as the style and http as the transport protocol. <wsdl:operation name="hello"> name defines operation associated with each portType. Each operation will have a soapAction associated with it. <wsdl:input name="helloRequest"> define the input for the operation and <wsdlsoap:body use="literal"/> defines the encoding standard. In our example we are using literal as the 14 encoding standard.

Web Services escription 0anguage )WS 0+ )3+


<service>
Service element defines where the service can be accessed. Each service is associated with a unique name which is suggested by <wsdl:service name="HelloWorldService"> in the example. The next element <wsdl:port binding="intf:HelloWorldSoapBinding" name="HelloWorld"> suggests the previous binding which will be used by service. The element <wsdlsoap:address location="http://localhost:8080/SimpleWeb/services/HelloWorld"/> suggests the physical address through which a service can be accessed.

/. 45S6%ul Web Services )*+


In Java EE 6, JAX-RS provides the functionality for Representational State Transfer (RESTful) web services.
REST is well suited for basic, ad hoc integration scenarios RESTful web services, often better integrated with HTTP than SOAP-based services are, do not require XML messages or WSDL serviceAPI definitions

Project Jersey is the production-ready reference implementation for the JAX-RS specification
Jersey implements support for the annotations defined in the JAX-RS specification, making it easy for developers to build RESTful web services with Java and the Java Virtual Machine (JVM)

Because RESTful web services use existing well-known W3C and Internet Engineering Task Force (IETF) standards (HTTP, XML, URI, MIME) and have a lightweight infrastructure that allows services to be built with minimal tooling, developing RESTful web services is inexpensive and thus has a very low barrier for adoption
You can use a development tool such as NetBeans IDE to further reduce the complexity of developing RESTful web services
15 16

45S6%ul Web Services ),+


A RESTful design may be appropriate when the following conditions are met:
The web services are completely stateless.
A good test is to consider whether the interaction can survive a restart of the server

45S6%ul Web Services )"+


Bandwidth is particularly important and needs to be limited
REST is particularly useful for limited-profile devices, such as PDAs and mobile phones, for which the overhead of headers and additional layers of SOAP elements on the XML payload must be restricted

A caching infrastructure can be leveraged for performance


If the data that the web service returns is not dynamically generated and can be cached, the caching infrastructure that web servers and other intermediaries inherently provide can be leveraged to improve performance

Web service delivery or aggregation into existing web sites can be enabled easily with a RESTful style
Developers can use such technologies as JAX-RS and Asynchronous JavaScript with XML (AJAX) and such toolkits as Direct Web Remoting (DWR) to consume the services in their web applications Rather than starting from scratch, services can be exposed with XML and consumed by HTML pages without significantly refactoring the existing web site architecture Existing developers will be more productive because they are adding to something they are already familiar with rather than having to start from scratch with new technology

The service producer and service consumer have a mutual understanding of the context and content being passed along
Because there is no formal way to describe the web services interface, both parties must agree out of band on the schemas that describe the data being exchanged and on ways to process it meaningfully In the real world, most commercial applications that expose services as RESTful implementations also distribute so-called value-added toolkits that describe the interfaces to developers in popular programming 17 languages

18

/'. eveloping !lient %or 45S6%ul Web Service


http://geronimo.apache.org/GMOx O!"#/developing$client$%or$rest%ul$&eb$ service.html

6asks
1. Create a Dynamic Web Project to consume the Web Service 2. Developing the Web based Client 3. Setting Up the Deployment Plan 4. Deploying and Testing the Web Client Deploy , Testing

This tutorial will take you through the steps required in developing, deploying and testing a RESTful Web Service Client in Apache Geronimo for a web services which are already deployed on the server We will be creating a Web based Client which can access the RESTful web service via GET and POST methods. You can easily create this client without any external tools except that of a server environment. To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.
Sun JDK 5.0+ (J2SE 1.5) Apache Geronimo 2.x Eclipse IDE for Java EE Developers - Europa release Geronimo Eclipse Plug-in 2.x
http://geronimo.apache.org/apache-geronimo-v301-release.html
19

20

You might also like