Вы находитесь на странице: 1из 45

Antaryami Sahoo

Slides 45 Duration 1 hr


Services Basics Intro to Apache Axis

Web Services Basics What is Web Service? Web Services Architecture Ways to develop Web services XML Messaging XML-RPC SOAP What is WSDL? Development plan for Service Requestor Development plan for Service Provider


to Apache Axis

What is Apache Axis? Features of Apache Axis Installing Apache Axis

Publishing Web Service through Apache Axis

Walkthrough of deploying and accessing a simple

web service using Apache Axis

A Web Service is any service that

is available over the web uses standardized XML messaging is OS and Programming language independent

There are two ways we can view Web Services architecture

1. Web Service Roles 2. Web Service Protocol Stack

There are three major roles

Service Registry

Logically Centralized directory of services

2) Discover services

1) Register service

Service Requestor
Consumer of the Web Service

3) Invoke service

Service Provider
Provider of the Web Service



Responsible for centralizing services



Responsible for describing the public interface to a specific web service

XML Messaging


Responsible for encoding messages in common XML format



Responsible for transporting messages

1. 2.


REST ( Representative State Transfer) :

In RESTful web services, the emphasis is on simple point-topoint communication over HTTP using plain old XML It uses protocol methods for its operations like :

HTTP CRUD Equivalent :GET read POST update PUT create DELETE delete

A Simple RESTful Service

One example is the stock quote service as a RESTful web service . The request: GET /StockPrice/IBM HTTP/1.1 Host: example.org Accept: text/xml Accept-Charset: utf-8 The response:

HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <s:Quote xmlns:s="http://example.org/stock-service"> <s:TickerSymbol>IBM</s:TickerSymbol> <s:StockPrice>45.25</s:StockPrice> </s:Quote>

There are two ways of XML Messaging XML-RPC



a simple protocol that uses XML messages to perform RPC Request are encoded in XML and send via HTTP Response are encoded in XML and received via HTTP is a easiest way to get started with web services

<methodCall> <methodName> com.agram.sayHello </methodName> <params> <param> <value>Java</value> </param> </params> </methodCall>

<methodResponse> <params> <param> <value> <string>Hello Java</string> </value> </param> </params> </ methodResponse >


Object Access Protocol SOAP is slightly more complicated than the XML-RPC SOAP extended XML-RPC It uses XML namespaces and XML Schemas.

Envelope is like a SOAP Message wrapper for content Header is a optional Envelope element that could contain control information Header Body element includes requests and responses Body element will include Body a Fault element in the event of an error

GET /StockPrice HTTP/1.1 Host: example.org Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:s="http://www.example.org/stock-service"> <env:Body> <s:GetStockQuote> <s:TickerSymbol>IBM</s:TickerSymbol> </s:GetStockQuote> </env:Body> </env:Envelope>

HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn

<?xml version="1.0"?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:s="http://www.example.org/stock-service"> <env:Body> <s:GetStockQuoteResponse> <s:StockPrice>45.25</s:StockPrice> </s:GetStockQuoteResponse> </env:Body> </env:Envelope>

The <Fault> is the standard element for error handling. When present, it is the only child element of the SOAP <Body>. The structure of a fault looks like:

<env:Fault xmlns:m="http://www.example.org/timeouts"> <env:Code> <env:Value>env:Sender</env:Value> <env:Subcode> <env:Value>m:MessageTimeout</env:Value> </env:Subcode> </env:Code> <env:Reason> <env:Text xml:lang="en">Sender Timeout</env:Text> </env:Reason> <env:Detail> <m:MaxTime>P5M</m:MaxTime> </env:Detail> </env:Fault>

Web Services Description Language


XML-based language for describing Web Services

what the service does (description)

how to use it (method signatures)

where to find the service

does not depend on the underlying protocol But: It is not much human-readable

1. 2. 3. 4. 5.

Has 6 major elements

definitions defines the name of the web service types describes all the data types that will be transmitted message defines the name of the message that will be transmitted portType defines the operations binding defines how the message will be transmitted service defines where the service is located


Universal Description, Discovery and Integration

http://www.uddi.org UDDI creates a platform-independent, open framework & registry for: Describing services Discovering businesses Integrating business services The UDDI may be less used than predicted, especially on the Internet level

1) Find web service via UDDI

2) Retrieve service description file

3) Create XML-RPC or SOAP client

4) Invoke remote service

1) Create the core functionality

2) Create XML-RPC or SOAP service wrapper

3) Create service description file

4) Deploy service

5) Register new service via UDDI

Axis is essentially a SOAP engine a framework for constructing SOAP processors such as clients , servers, gateways etc - Axis Website

Successor to Apache SOAP It can run as a standalone server

or as an a server that plugs into Servlet engine like Tomcat Automatic WSDL generation for deployed services Java2WSDL to generate WSDL from Java interface WSDL2Java to generate Java classes from WSDL Easy deployment

1) Download Axis Distribution archive from http://xml.apache.org/axis/ - Axis distribution archive contains a web application ( webapps/axis/ directory) for hosting an Axis Server in a web container like Tomcat

2) Deploy Axis Server in the Tomcat 5.X server

a) Copy Axis Web application (webapps/axis directory) to $TOMCAT_HOME/webapps b) Copy jaxrpc.jar (that is provided with Axis distribution) and xerces.jar ( or any other XML parser) to $TOMCAT_HOME/common/lib
jaxrpc.jar and xerces.jar includes classes with java and javax packages and Tomcat does not authorize to load any classes in that package from WEB-INF/ lib directory of the web application.

3) Configure the environment by including these libraries in the CLASSPATH - log4j-core.jar - commons-logging.jar - wsdl4j.jar - jaxrpc.jar - tt.bytecode.jar - xerces.jar ( or any other XML Parser)

1) 2)

Start the Tomcat Web Server Goto http://localhost:8080/axis/

- you should be able to see Apache-Axis start page - if you did not , then the axis is not correctly installed or the web server is not running


Goto http://localhost:8080/axis/happyaxis.jsp
- this test page verifies whether all the needed and optional libraries are present. - Axis will not perform properly until all the needed libraries are present.

The two ways we can publish a web service with Axis are,
1. Instant Deployment Java Web Service (JWS) 2. Custom Deployment Using Web Service Deployment Descriptor ( WSDD)


Code code a simple HelloWorld java class that we want to expose as web service Java2WSDL Generate the WSDL file for the given HelloWorld Interface



WSDL2Java Generate the Server side wrapper class and stubs for easy client access
Deploy deploy the service to apache axis



Client code a simple client that access our HelloWorld Web Service

HelloWorld.java package helloworld; public interface HelloWorld { public String sayHello( String name); }

package helloworld; public class HelloWorldImpl implements HelloWorld {

public String sayHello( String name){ if(name == null) return Hello Everyone; else return Hello + name; }

This command will generate the WSDL that Conforms to our interface
% java org.apache.axis.wsdl.Java2WSDL -o hello.wsdl -l http://localhost:8080/axis/services/helloworld -n urn:helloworld -phelloworld" urn:helloworld helloworld.HelloWorld Parameters description -o = Name of the output -l = URL of the web Service -n = Target Namespace for the WSDL -p = Map Java package to namespace Fully Qualified Class Name

This command will generate the wrapper code for deploying the service, as well as client stubs for accessing it.
% java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p helloworld.gen hello.wsdl Parameters description -o = Base output Directory -d = Scope of deployment -s = To generate Server-side code too -p = Package to place the code Name of the WSDL

These are the codes that will get generated


HelloWorldSoapBindingImpl.java This is the implementation code for the Web Service HelloWorld.java This is the remote interface HelloWorldService.java This is the service interface HelloWorldServiceLocator.java Helper class to retrieve handler to service HelloWorldSoapBindingSkeleton.java Server-side skeleton code HelloWorldSoapBindingStub.java Client side stub deploy.wsdd axis deployment descriptor undeploy.wsdd deployment descriptor to undeploy the web services from the Axis System

2. 3. 4. 5. 6. 7. 8.

HelloWorldSoapBindingImpl package helloworld.gen; import helloworld.HelloWorldImpl; public class HelloWorldSoapBindingImpl implements helloworld.gen.HelloWorld { HelloWorldImpl helloWorld = new HelloWorldImpl(); public String sayHello(String str0) throws java.rmi.RemoteException {


return helloWorld.sayHello(str0);


Compile the Service Code

% javac helloworld\gen\*.java


Package the code for Axis

% jar cvf hello.jar helloworld/*.class helloworld/gen/*.class % mv hello.jar $TOMCAT_HOME/webapps/axis/WEBINF/lib


Deploy the Service using WSDD

% java org.apache.axis.client.AdminClient deploy.wsdd
<admin> Done processing </Done>

package helloworld; Import helloworld.gen.*;

public class HelloWorldTester {

public static void main(String [] args) throws Exception { // Make a service HelloWorldService service = new HelloWorldServiceLocator(); // Now use the service to get a stub to the service helloworld.gen.HelloWorld hello = service.getHelloWorld(); } // Make the actual call System.out.println( hello.sayHello(Java Gurus) );

Web Service


Roles in Web Services Web Services Protocol Stack Different ways of XML Messaging Development plan for Service Requestor and Provider

Architecture of Axis Features of Apache Axis Installing Apache Axis Different ways of deploying Web Service with Axis Deploying and accessing a simple web service using Apache Axis


Services & Java home http://java.sun.com/j2ee/webservices/index.html Java Web Services tutorial http://java.sun.com/xml/docs.html#tutorials Apache Axis http://xml.apache.org/axis/index.html SOAP 1.1 - http://www.w3.org/TR/SOAP WSDL 1.1 - http://www.w3.org/TR/wsdl