You are on page 1of 54

Developing Web Services with Axis

Web Languages Course 2009 University of Trento

Lab Objective

Develop and Deploy Web Services (serverside)

23 March 2009

Gaia Trecarichi - Web Languages Course

Lab Outline

WS Sum Up:

WS-protocols Axis Functionalities WSDL2Java Tool

Web Services Deployment

JWS WSDD

Web Services Development

Skeleton Generation (WSDL2Java tools) WSDL Generation


Gaia Trecarichi - Web Languages Course

23 March 2009

Web Service Sum-up

web service protocols

Web Services Protocols

REST (Representational State Transfer): uses plain old HTTP to make method calls and you get XML back.
XML-RPC: is a remote procedure call protocol which uses XML for marking up both requests and responses. SOAP (Simple Object Access Protocol): is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks.

23 March 2009

Gaia Trecarichi - Web Languages Course

WS protocol stack

23 March 2009

Gaia Trecarichi - Web Languages Course

XML- messaging

23 March 2009

Gaia Trecarichi - Web Languages Course

XML- messaging

23 March 2009

Gaia Trecarichi - Web Languages Course

Big Web Service

Is available over the Internet or private (intranet) networks Uses a standardized XML messaging system Is not tied to any one operating system or programming language Is self-describing via a common XML grammar (WSDL) Is discoverable via a simple find mechanism (UDDI)
23 March 2009 Gaia Trecarichi - Web Languages Course

WS architecture

23 March 2009

Gaia Trecarichi - Web Languages Course

Web Service Sum-up

Axis functionalities

General Web Service Toolkit Functionality

23 March 2009

Gaia Trecarichi - Web Languages Course

Toolkit Functionality for

Today Class

23 March 2009

Gaia Trecarichi - Web Languages Course

WSDL2Java Tool

Stub classes from a Java interface/class *:

1. 2. 3. 4. 5. 6.
server

7.

*.java: New interface file with appropriate remote usages. *Service.java: client side service interface. *ServiceLocator.java: client side service implementation factory. *SoapBindingStub.java: Client side stub. (data types): Java files produced for non-standard types. *SoapBindingImpl.java: default server implementation WS, modify manually to actual implementation. *SoapBindingSkeleton.java: Server side skeleton.

Deploy.wsdd / undeploy.wsdd: (Un-)deployment descriptors


23 March 2009 Gaia Trecarichi - Web Languages Course

Relationships Between Generated Files

23 March 2009

Gaia Trecarichi - Web Languages Course

WSDL2Java Generates

For each WSDL file

Deployment descriptor called deploy.wsdd

used by AdminClient to deploy the service implementation class referred to will either be the skeleton
class (if skeleton generated) or the impl. class (if no skeleton generated) can change to refer to your own class

Undeployment descriptor called undeploy.wsdd


used by AdminClient to undeploy the service

23 March 2009

Gaia Trecarichi - Web Languages Course

Using WSDL2Java

Command

java org.apache.axis.wsdl.WSDL2Java wsdl-uri

Option highlights -Nnamespace=package : maps a namespace to a Java package -o dir : specifies output directory where generated files should
be written -p pkg : overrides default package name for generated classes -s : generates service-side interfaces and classes required by a service implementation, including a skeleton class -t : generates JUnit test case for the web service -v : prints a message about each file that is generated
23 March 2009 Gaia Trecarichi - Web Languages Course

Web Services Deployment

Simple Deployment (JWS) WSDD Deployment

Deploying Java Web Services (JWS)

Steps

If

not there already, copy axis directory in webapps directory of the Axis distribution to the deployment directory of a server that supports Java servlets

Copy any Java source file that implements a


web service into this axis directory

For Tomcat, this is the webapps directory No special code is required All public, non-static methods are exposed
Gaia Trecarichi - Web Languages Course

23 March 2009

Deploying Java Web Services (JWS)

Change the file extension from .java to .jws To view the WSDL of a JWS web service, enter
the following URL in a web browser
http://host:port/axis/file-name.jws?wsdl
JWSHandler, JWSProcessor RPCProvider classes do the job

A file .class is created under the dir

\webapps\axis\WEB-INF\jwsClasses

23 March 2009

Gaia Trecarichi - Web Languages Course

Java Web Services (JWS)

Pros

Simple to create Simple to deploy Simple to start with

Cons

No

user defined binding of service name and implementation class No WSDL

Hard to publish Hard to administrate No remote transparency

23 March 2009

Gaia Trecarichi - Web Languages Course

Customized Deployment

Benefits over JWS deployment

deploy/undeploy multiple services deploy/undeploy handlers deploy classes with no source custom type mapping these are web service implementation classes and classes they use when using Axis webapp

Server-side classes

copy .class files to webapps/axis/WEB-INF/classes copy .jar files to webapps/axis/WEB-INF/lib


Gaia Trecarichi - Web Languages Course

23 March 2009

Customized Deployment

Web Service Deployment Descriptor (WSDD)

Specifies components to be deployed and undeployed typically called Specifies type mappings
deploy.wsdd or undeploy.wsdd

AdminClient uses WSDD

To deploy or undeploy components, pass it a WSDD file


java org.apache.axis.client.AdminClient filename.wsdd

modifies server-config.wsdd in the axis webapp WEB-INF directory


To list deployed components
java org.apache.axis.client.AdminClient list

outputs server-config.wsdd which is used when the server is restarted to redeploy all previously deployed services
Gaia Trecarichi - Web Languages Course

23 March 2009

WSDD To Deploy

Generated by WSDL2Java Example to deploy a service

identifies the pivot handler to be used; Axis provides three: RPC, MSG and EJB; MSG is for message or document-centric calls as opposed to RPC-style calls

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="CarQuote" provider="java:RPC"> don t need to <parameter name="className" have source value="com.ociweb.auto.CarQuoteImpl"/> for this class <parameter name="allowedMethods" value="*"/> </service> </deployment>
makes all public, non-static methods of the class available; can be a whitespacedelimited list of method names 23 March 2009 Gaia Trecarichi - Web Languages Course

WSDD To Undeploy

Generated by WSDL2Java Example to undeploy a service

<undeployment xmlns="http://xml.apache.org/axis/wsdd/"> <service name="CarQuote"/> </undeployment>

23 March 2009

Gaia Trecarichi - Web Languages Course

Java Bean Registration with WSDD


For Automatic Serialization Example for the Car class

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="CarQuote" provider="java:RPC"> beanMapping is <parameter name="className needed if a Car value="com.ociweb.cardealer.CarQuoteImpl"/> object is passed to or returned from <parameter name="allowedMethods" value="getQuote"/> a web service </service> operation <beanMapping qname="ns:Car" xmlns:ns="urn:CarQuote" languageSpecificType="java:com.ociweb.cardealer.Car"/> </deployment>
namespace 23 March 2009 Gaia Trecarichi - Web Languages Course

Custom Type Mapping

Steps

write serializer class

implements org.apache.axis.encoding.Serializer implements org.apache.axis.encoding.SerializerFactory

write serializer factory class that returns serializer instances

write deserializer class

extends org.apache.axis.encoding.DeserializerImpl implements org.apache.axis.encoding.DeserializerFactory


Gaia Trecarichi - Web Languages Course

write deserializer factory class that returns deserializer instances

23 March 2009

Custom Type Mapping

Register them using WSDD and the Admin client

<typeMapping xmlns:ns="urn:CarQuote" qname="ns:Car" type="java:com.ociweb.cardealer.Car" serializer="com.ociweb.cardealer.CarSerializerFactory" deserializer="com.ociweb.cardealer.CarDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> </typeMapping> add as child of deployment element

23 March 2009

Gaia Trecarichi - Web Languages Course

Web Services Development


Server-side

classes generation

WSDL2Java tool

WSDL

Generation Exercises

Server-side classes Generation

Steps

Take a WSDL file, that is, the description of a service.

Use WSDL2Java tool for generating WSDD files, Skeleton (optional) and Implementation class:

java

-cp %AXISCP% org.apache.axis.wsdl.WSDL2Java AddFunction.wsdl o src s [-S true]

Modify the *Impl.java file according to your implementation

Compile the server-side classes (*PortType/*Impl/ *Skeleton.java).

javac

-d classes src\localhost\axis\AddFunction_jws\*.java

23 March 2009

Gaia Trecarichi - Web Languages Course

WSDD Deployment

Copy the generated classes under ../axis/WEB-INF/classes Startup tomcat

Run

\apache-tomcat-5.5.26\bin\startup.bat

Undeploy the service

java java Run Run

-cp %AXISCP% org.apache.axis.client.AdminClient src\localhost\axis\AddFunction_jws\undeploy.wsdd -cp %AXISCP% org.apache.axis.client.AdminClient src\localhost\axis\AddFunction_jws\deploy.wsdd \apache-tomcat-5.5.26\bin\shutdown.bat \apache-tomcat-5.5.26\bin\startup.bat
Gaia Trecarichi - Web Languages Course

Deploy the service

[optional but adviced] Shutdown tomcat [optional but adviced] Startup tomcat
23 March 2009

Exercise 1

Realize the steps described before, taking as a WSDL file AddFunction.wsdl

You should see something like that Click on wsdl link Compare the browsed wsdl with the original Where is the difference?

23 March 2009

Gaia Trecarichi - Web Languages Course

Web Service Development

There are three basic methods:

1. Instant Deployment (JWS)



Generates a WSDL from an URL For simple services

2. WSDD Deployment (manual wsdd creation)


Generates a WSDL from an URL

3. WSDD Deployment (automatic wsdd creation)


Use java2wsdl to generate the WSDL from Java service implementation classes
Gaia Trecarichi - Web Languages Course 23 March 2009

Web Service Development

Steps

1. Create the code for your service 2. Create the wsdd files

(deploy.wsdd

and

undeploy.wsdd) 3. Compile the service code 4. Copy the classes under /axis/WEB-INF/classes 5. Deploy the service with AdminClient 6. [optional] Create the service stubs (with WSDL2Java tool)
Note: [optional but advised] Use an Ant Task to automate the steps 3-6
23 March 2009 Gaia Trecarichi - Web Languages Course

Exercises

Developing web services

Exercise 2

Steps

Download the source code Open the project Lab7 Have a look to the files Calculator.java, deploy.wsdd, undeploy.wsdd, build.xml Change the properties (files build.properties) according to your settings Run the ant task deploy A service named Calculator should be created Check on the Axis page Run the ant task create-stubs see what happens Write a client to test the new deployed service
23 March 2009 Gaia Trecarichi - Web Languages Course

Exercise 3

Make a service that

Given: a customer order Return: a string with info concerning the order Hints & Advices: the customer order is represented by a Java Bean class which contains, among the others,
info like the customer name, the shipping address, a list of items to buy and respective quantities Create a new project (i.e., Order) Copy the files build.xml and *.properties from the project Lab7

Deploy the service with your own wsdd file

Hints: remember to enclose in the wsdd file the type mapping using the
beanMapping tag

Generate the service stubs (for next exercise)


23 March 2009 Gaia Trecarichi - Web Languages Course

Exercise 4

Build a Client to test the Service of Exercise 7

1.

Make a Client using DII

Hints: add the following lines of code in the main of the Client class

Qname qn = new QName( YourBeanNameSpace", YourBeanName" ); call.registerTypeMapping(YourBeanName.class, qn, new org.apache.axis.encoding.ser.BeanSerializerFactory(YourBeanName.class, qn), new org.apache.axis.encoding.ser.BeanDeserializerFactory(YourBeanName.class, qn));

2.

Make a Client using Service Stubs

23 March 2009

Gaia Trecarichi - Web Languages Course

Web Service Development

There are three basic methods:

1. Instant Deployment (JWS)



Generates a WSDL from an URL For simple services

2. WSDD Deployment (manual wsdd creation)


Generates a WSDL from an URL

3. WSDD Deployment (automatic wsdd creation)


Use java2wsdl to generate the WSDL from Java service implementation classes
Gaia Trecarichi - Web Languages Course 23 March 2009

Build.xml: axis-java2wsdl
<!-=============== JAVA 2WSDL SECTION =============== --> <target name="create-wsdl" depends="do-jar" description="gen WSDL from java"> <mkdir dir="${wsdl}" /> <axis-java2wsdl classname="yourClassName" namespace="http://localhost:8080/axis/services/yourService" location="http://localhost:8080/axis/services/yourService" style="RPC" output="${j2w.wsdl}"> <classpath> <path path="${wsdl.classpath}" /> </classpath> </axis-java2wsdl> </target>
5/5/2009 Gaia Trecarichi - Web Languages Course 40

Web Service Development: use of Java2Wsdl tool


1.

Starting from you service implementation, create a wsdl file


Use ant task create-wsdl

In the project.properties file:


#for java2wsdl j2w.classname=myservice.MyServiceImpl j2w.namespace=http://myservice.NET j2w.location=http://${target.server}:${target.port}/${targe t.appname}/services/${service.name} j2w.style=RPC wsdl=wsdl j2w.wsdl=${wsdl}/${service.name}.wsdl wsdl.classpath=${build.service.local}
23 March 2009 Gaia Trecarichi - Web Languages Course

Web Service Development: use of Java2Wsdl tool


2.

Given the wsdl just created, generate the stub classes


Use ant task create-stubs (which make use of wsdl2java tool)

Notice that you do this for two reasons: - to generate the deploy.wsdd and the undeploy.wsdd * - to generate the stub classes (client side) to be imported in my tester class

* The option serverside="true must be enabled in order to obtain the .wsdd files

23 March 2009

Gaia Trecarichi - Web Languages Course

Web Service Development: use of Java2Wsdl tool


3.

In the wsdd file generated, change the line


<parameter name="className" value= *SoapBindingImpl> into the line: <parameter name="className" value= myservice.MyServiceImpl> :

Notice that when the wsdd is automatically generated, Axis puts "*SoapBindingImpl" because it takes the name from the skeleton class (which you don't use because you have already your service implementation). Actually, the class where the service is implemented is myservice.MyServiceImpl, that's why you have to change it.

23 March 2009

Gaia Trecarichi - Web Languages Course

Web Service Development: use of Java2Wsdl tool


4.

[optional, for cleaness purposes]: put the .wsdd files under the directory "deployment" of your project

Notice that if you skip this step, you have to set the proper lines in the project.properties file to tell Axis where are the .wsdd files: #directory containing the deployment files deployment.wsdd=deployment/deploy.wsdd undeployment.wsdd=deployment/undeploy.wsdd

23 March 2009

Gaia Trecarichi - Web Languages Course

Web Service Development: use of Java2Wsdl tool


5.

Deploy the service


Use ant task deploy

6.

Test the service with a Client


Notice that you already have the stub jar (created in step 2)

23 March 2009

Gaia Trecarichi - Web Languages Course

Exercise 5

As in Exercise3, make a service that

Given: a customer order Return: a string with info concerning the order Hints & Advices: Create a new project (i.e., Order1) Copy the files build.xml and *.properties from the project Lab7
Hints: use target create-wsdl of build.xml

Use Java2WSDL tool to create the wsdl file Create *.wsdd files and modify deploy.wsdd properly Deploy the service Generate the service stubs and make a test client
23 March 2009 Gaia Trecarichi - Web Languages Course

Some Remarks

Use Java Beans when you want to pass complex data types as input/output parameters for your own web services.

Axis doesnt mind if you nest Java Beans as much as you want

Dont try to pass ArrayList types. Use the Java arrays instead.

23 March 2009

Gaia Trecarichi - Web Languages Course

References

Axis User Guide

Provided in the course website

http://www.ibm.com/developerworks/webservices/library/w s-whichwsdl/#listing7

Which style of WSDL should I use?

W3Cs Web Services Activity Home Page: http://www.w3.org/2002/ws/


23 March 2009 Gaia Trecarichi - Web Languages Course

Troubleshooting

Ant Problems

Eclipse

seems to recognize an Ant file (i.e., build.xml) but cannot actually run its targets It may be a problem of Eclipse version, ant version, how Eclipse was installed. Actions

If

you have Ubuntu, avoid the Eclipse packages provided by Ubuntu. Install Eclipse by downloading it from the official website Run the ant file from the command line Tell eclipse to use an external ant installation and see what happens (last optionso far)
23 March 2009 Gaia Trecarichi - Web Languages Course

Ant Problems

It

doesnt calculate the correct path according to the given settings

Example:

It may be a problem of Ant version (?) Action


file) file:///C://blabla//file.wsdl

you fixed . as a basedir property in your build.xml file and some files (i.e, the wsdl file), indicated together with a relative path (i.e, wsdl) in a .properties file (i.e, stub.properties), are not accessible.

put the absolute path for the file in question (i.e., wsdl
23 March 2009 Gaia Trecarichi - Web Languages Course

WSDL Problems

Make sure you save the WSDL file in the correct


way

DONT SAVE

COPY AND PAST THE CONTENT OF THE WEB PAGE THE FILE AS AN XML FILE AND CHANGE THE EXTENSION TO WSDL

23 March 2009

Gaia Trecarichi - Web Languages Course

Axis Problems

Make sure the 14 axis libraries (*.jar) are placed under %TOMCAT_HOME%/webapps/axis/WEB-INF/lib List of the libraries: activation.jar
axis-ant.jar axis.jar commons-discovery-0.2.jar commons-logging-1.0.4.jar jaxp-1.3.jar jaxrpc.jar log4j-1.2.8.jar mail.jar saaj.jar servlet-api.jar wsdl4j-1.5.1.jar xml-apis.jar, tool.jar

23 March 2009

Gaia Trecarichi - Web Languages Course

WSDD Deployment in Linux

Type the command env to display your Unix environment variables If not there, set the environmental variable AXISLIB with the command export

export

AXISLIB=/webapps/axis/WEB-INF/lib

Type the following command to deploy a service

java -cp $AXISLIB/activation.jar:$AXISLIB/axis.jar:$AXISLIB/commons-discovery0.2.jar:$AXISLIB/commons-logging1.0.4.jar:$AXISLIB/jaxrpc.jar:$AXISLIB/log4j1.2.8.jar:$AXISLIB/mail.jar:$AXISLIB/saaj.jar:$AXISLIB/wsdl4j1.5.1.jar:$AXISLIB/xml-apis.jar:$AXISLIB/axis-ant.jar Usually 8080 org.apache.axis.client.AdminClient -h yourhostname -p tomcatport /fulldeploypath/deploy.wsdd Usually 127.0.0.1 23 March 2009 Gaia Trecarichi - Web Languages Course

You might also like