Handling%20Web%20Services%20with%20Apache%20AXIS - PowerPoint PPT Presentation

About This Presentation
Title:

Handling%20Web%20Services%20with%20Apache%20AXIS

Description:

A simple stand-alone server. A Web application that ... the WSDL URL, the service name, the namespace uri, the operation name and the ... How Does it Work? ... – PowerPoint PPT presentation

Number of Views:266
Avg rating:3.0/5.0
Slides: 72
Provided by: Ben5152
Category:

less

Transcript and Presenter's Notes

Title: Handling%20Web%20Services%20with%20Apache%20AXIS


1
Handling Web Services with Apache AXIS
2
Web-Service Review
  • By Examples ...

3
POST /wsprimegenerator.exe/soap/IPrimeGenerator
HTTP/1.0 Content-Type text/xml
charsetutf-8 Host www.jusufdarmawan.com SOAPActi
on "urnUnitIPrimeGenerator-IPrimeGeneratorprime
generator" Content-Length 527 lt?xml
version"1.0" encoding"UTF-8"?gt ltsoapenvEnvelope
xmlnssoapenv"http//schemas.xmlsoap.org/s
oap/envelope/" xmlnsxsd"http//www.w3.org/2
001/XMLSchema" xmlnsxsi"http//www.w3.org/2
001/XMLSchema-instance"gt ltsoapenvBodygt
ltns1primegenerator soapenvencodingStyle
"http//schemas.xmlsoap.org/soap/encoding/"
xmlnsns1"urnUnitIPrimeGenerator-IPrim
eGenerator"gt ltvalstart
xsitype"xsdstring"gt12lt/valstartgt
ltvalend xsitype"xsdstring"gt120lt/valendgt
lt/ns1primegeneratorgt lt/soapenvBodygt lt/soape
nvEnvelopegt
A request to www.jusufdarmawan.com
4
HTTP/1.0 200 OK Date Mon, 09 May 2005 205835
GMT Content-Length 619 Content-Type
text/xml Server Microsoft-IIS/5.0 X-Powered-By
ASP.NET lt?xml version"1.0" encoding'UTF-8'?gt
ltSOAP-ENVEnvelope xmlnsSOAP-
ENV"http//schemas.xmlsoap.org/soap/envelope/"
xmlnsxsd"http//www.w3.org/2001/XMLSchema
" xmlnsxsi"http//www.w3.org/2001/XMLSche
ma-instance" xmlnsSOAP-ENC"http//schemas
.xmlsoap.org/soap/encoding/"gt
ltSOAP-ENVBodygt ltNS1primegeneratorRespons
e xmlnsNS1"urnUnitIPrimeGenerator
-IPrimeGenerator"
SOAP-ENVencodingStyle"http//schemas.xmlsoap.org
/soap/encoding/"gt ltreturn
xsitype"xsdstring"gt13,17,19,23,29,31, 37,
41,43, 47, 53,59,61,67,71,73,7
9,83,89,97,101,103,107,109,113lt/returngt
lt/NS1primegeneratorResponsegt
lt/SOAP-ENVBodygt lt/SOAP-ENVEnvelopegt
The Response
5
POST /soap HTTP/1.0 SOAPAction
"" Content-Length 520 lt?xml version"1.0"
encoding"UTF-8"?gt ltsoapenvEnvelope
xmlnssoapenv"http//schemas.xmlsoap.org/soap/e
nvelope/" xmlnsxsd"http//www.w3.o
rg/2001/XMLSchema"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce"gt ltsoapenvBodygt ltns1getRate
soapenvencodingStyle"http//schemas.xmlsoap.o
rg/soap/encoding/" xmlnsns1"urnxmethod
s-CurrencyExchange"gt ltcountry1
xsitype"xsdstring"gtEurolt/country1gt
ltcountry2 xsitype"xsdstring"gtIsraellt/country2gt
lt/ns1getRategt lt/soapenvBodygt lt/soapenvEnvel
opegt
A request to services.xmethods.net
6
HTTP/1.0 200 OK Date Sat, 07 May 2005 232621
GMT Content-Length 492 Content-Type
text/xml lt?xml version'1.0' encoding'UTF-8'?gt lt
soapEnvelope xmlnssoap'http//schemas.xml
soap.org/soap/envelope/'
xmlnsxsi'http//www.w3.org/2001/XMLSchema-instan
ce' xmlnsxsd'http//www.w3.org/2001/XMLS
chema' xmlnssoapenc'http//schemas.xmlsoap
.org/soap/encoding/' soapencodingStyle'htt
p//schemas.xmlsoap.org/soap/encoding/'gt
ltsoapBodygt ltngetRateResponse
xmlnsn'urnxmethods-CurrencyExchange'gt
ltResult xsitype'xsdfloat'gt5.5825lt/Res
ultgt lt/ngetRateResponsegt
lt/soapBodygt lt/soapEnvelopegt
The Response
7
A WSDL Example
lt?xml version"1.0"?gt ltdefinitions
name"CurrencyExchangeService"
targetNamespace"http//www.xmethods.net/sd/Curren
cyExchangeService.wsdl" xmlnstns"http//www.x
methods.net/sd/CurrencyExchangeService.wsdl"
xmlnsxsd"http//www.w3.org/2001/XMLSchema"
xmlnssoap"http//schemas.xmlsoap.org/wsdl/soap/"
xmlns"http//schemas.xmlsoap.org/wsdl/"gt
ltmessage name"getRateRequest"gt ltpart
name"country1" type"xsdstring"/gt ltpart
name"country2" type"xsdstring"/gt
lt/messagegt ltmessage name"getRateResponse"gt
ltpart name"Result" type"xsdfloat"/gt
lt/messagegt ltportType name"CurrencyExchangePort
Type"gt ltoperation name"getRate"gt
ltinput message"tnsgetRateRequest"
name"getRate"/gt ltoutput
message"tnsgetRateResponse" name"getRateRespo
nse"/gt lt/operationgtlt/portTypegt
8
ltbinding name"CurrencyExchangeBinding"
type"tnsCurrencyExchangePortType"gt
ltsoapbinding style"rpc" transport"http//schema
s.xmlsoap.org/soap/http"/gt ltoperation
name"getRate"gt ltsoapoperation
soapAction""/gt ltinput name"getRate"gt
ltsoapbody use"encoded" namespace"urnxmethods-
CurrencyExchange"
encodingStyle"http//schemas.xmlsoap.org/soap/enc
oding/"/gt lt/inputgt ltoutput
name"getRateResponse"gt ltsoapbody
use"encoded" namespace"urnxmethods-CurrencyExch
ange" encodingStyle"http//schemas.
xmlsoap.org/soap/encoding/"/gt lt/outputgt
lt/operationgt lt/bindinggt ltservice
name"CurrencyExchangeService"gt
ltdocumentationgtReturns the exchange rate between
the two currencieslt/documentationgt ltport
name"CurrencyExchangePort" binding"tnsCurrencyE
xchangeBinding"gt ltsoapaddress
location"http//services.xmethods.net80/soap"/gt
lt/portgt lt/servicegt lt/definitionsgt
9
WSDL Elements
Service
Port

Binding
Input
Output
Port Type
Operation

10
Apache EXtensible Interaction System(Axis)
11
What is AXIS
  • Axis is essentially a SOAP engine a framework
    for constructing SOAP processors
  • client side
  • server side
  • Axis implements the interfaces of JAX-RPC
    (XML-based remote procedure calls in Java)
  • AXIS Apache EXtensible Interaction System

12
Apache AXIS Included Tools
  • A simple stand-alone server
  • A Web application that resides in Servlet
    engines, such as Tomcat
  • Standalone tools for Web-service invocation
  • Java/C components
  • Extensive support for the Web Service Description
    Language (WSDL)
  • E.g., tools for generating Java classes (stubs)
    from WSDL and vice versa
  • and more

13
Remote Method Invocation is not New
  • java.rmi has been in Java since Javas early
    versions
  • In Java RMI, objects can invoke methods of
    objects that reside on a remote computer
  • (RMI Remote Method Invocation)
  • So, what has been changed?
  • Using HTTP for communication
  • Using agreed protocols, Java can invoke methods
    that were not written in Java (e.g., .NET
    methods) and vice versa
  • A complex registry procedure has been required in
    RMI

14
What We Would Like to Create
  • Client applications applications that can call a
    remote Web service
  • Services methods that can be called by remote
    applications
  • Service descriptions WSDL files that describe
    our methods

15
Client Applications
16
Calling Web Services
  • By now, we already know how to invoke a remote
    Web service in Java
  • Open a socket to the remote server
  • Through the socket, send a SOAP request wrapped
    by a HTTP request
  • Parse the response (e.g., using SAX/DOM)
  • However, this approach is cumbersome, and most of
    it can be automated
  • This is the whole point of using standards

17
Invoking Services with Apache
  • Axis includes comfortable tools for managing
    calls to Web services
  • The programmer configures the request for the Web
    service using a friendly API
  • with, or without considering the WSDL
  • According to the configuration, a method
    invocation is transformed into a SOAP request
    that is sent to the remote server

18
Invoking Services with Axis
  • To invoke an operation of a Web service, do the
    following
  • Construct a Service instance
  • Using the Service instance, generate a Call
    instance
  • Configure the Call instance
  • Invoke the call

19
Example Currency Exchange
import org.apache.axis.client. import
javax.xml.namespace.QName import
javax.xml.rpc.ParameterMode import
javax.xml.rpc.encoding.XMLType public class
CurrencyExchange public static void
main(String args) throws Exception
System.setProperty("http.proxyHost","wwwproxy.huji
.ac.il") System.setProperty("http.proxyPort","
8080") String endpointUrl
"http//services.xmethods.net80/soap" String
nsuri "urnxmethods-CurrencyExchange"
Service service new Service() Call call
(Call) service.createCall()
20
Example Currency Exchange (cont)
call.setTargetEndpointAddress(endpointUrl)
call.setOperationName(new QName(nsuri,"getRate
")) call.addParameter("country1",
XMLType.SOAP_STRING,
ParameterMode.IN)
call.addParameter("country2", XMLType.SOAP_STRING,

ParameterMode.IN) call.setReturnType(XMLTyp
e.SOAP_FLOAT) Object ret
call.invoke(new Object "Euro","Israel")
System.out.println(ret)
21
Using the WSDL
  • Axis can read a given WSDL and configure the
    service as much as possible from that WSDL
  • Instead of using the default constructor,
    construct the service using the following
    constructor
  • Service(String wsdlLocation, QName serviceName)
  • When there is only one port, we can obtain it
    automatically
  • In this approach, you usually need to know the
    following about the service
  • the WSDL URL, the service name, the namespace
    uri, the operation name and the expected arguments

22
public class CurrencyExchange2 public static
void main(String args) throws Exception
System.setProperty("http.proxyHost",wwwproxy.huji
.ac.il") System.setProperty("http.proxyPort",
"8080") String wsdl "http//www.xmethods.n
et/sd/2001/CurrencyExchangeService.wsdl"
QName sName new QName("http//www.xmethods.net/
sd/CurrencyExchangeService.wsdl",
"CurrencyExchangeService") String oName
"getRate" Service service new
Service(wsdl, sName) QName port
(QName)service.getPorts().next() Call call
(Call)service.createCall(port, oName)
System.out.println(call.invoke(new Object
"UK","Israel"))
23
public class PrimeNumbers public static void
main(String args) throws Exception
System.setProperty("http.proxyHost",wwwproxy.huji
.ac.il") System.setProperty("http.proxyPort",
"8080") String wsdl "http//www50.brinkste
r.com/vbfacileinpt/np.asmx?wsdl" QName sName
new QName("http//www50.brinkster.com/vbfacile
inpt/np","pnum") String oName
"GetPrimeNumbers" Service service new
Service(wsdl, sName) QName port
(QName)service.getPorts().next() Call call
(Call)service.createCall(port, oName)
System.out.println(call.invoke(new Object
100))
24
The WSDL2Java Application
  • Axis provides a mechanism for communicating with
    a Web service using stubs
  • That is, generation of regular Java classes that
    have an interface similar to that of the Web
    service and implementation that wraps Web service
    management
  • Invoke class WSDL2Java in order to create the
    required Java classes

25
Generated Classes
  • WSDL2Java generates the following
  • A service interface and a service implementation
    (locator) for each service
  • A stub class for each binding
  • An interface for each port type
  • This interface contains methods that correspond
    to the operations of the port type
  • A class for each complex type

26
An Example
27
Using The Generated Classes
import com.brinkster.www50.vbfacileinpt.np. publ
ic class PrimeNumbers2 public static void
main(String args) throws Exception
System.setProperty("http.proxyHost","wwwproxy.huji
.ac.il") System.setProperty("http.proxyPort",
"8080") PnumSoap ps new
PnumLocator().getpnumSoap()
System.out.println(ps.getPrimeNumbers("20"))

28
Server Applications
29
AXIS Installation
  • Axis works inside a Servlet container (e.g.,
    Tomcat)
  • You should add to your Tomcat libs some jar
    files axis.jar, saaj.jar, wsdl4j.jar,
  • You need to include several jar files in your
    CLASSPATH
  • This has already been done for you!
  • The needed CLASSPATH definitions where added to
    dbi.cshrc
  • The needed jar files are in CATALINA_HOME/lib/

30
AXIS Installation (cont)
  • You need to copy the Axis application to your
    Tomcat's application directory
  • cp -r dbi/tomcat/axis/ CATALINA_BASE/webapps

31
Creating a Web Service
  • Next, we will see how we can create and publish
    a Web service using the Axis plugin in Tomcat

32
1. Generate the Implementing Class
package myws public class Power
public int power(int a, int n)
return (int)Math.pow(a,n)
CATALINA_BASE/webapps/axis/WEB-INF/
classes/myws/Power.class
33
2. Deploy the Service using Web Service
Deployment Descriptor
ltdeployment xmlns"http//xml.apache.org/axis/wsdd
/" xmlnsjava"http//xml.apache.org/a
xis/wsdd/providers/java"gt ltservice
name"mypower" provider"javaRPC"gt
ltparameter name"className" value"myws.Power"/gt
ltparameter name"scope" value"application"
/gt ltparameter name"allowedMethods"
value""/gt lt/servicegt lt/deploymentgt
services.wsdd
java org.apache.axis.client.AdminClient
-hlocalhost -p8080 services.wsdd
34
That's it! You Can Call the Service.
import org.apache.axis.client. public class
PowerClient public static void
main(String argv)throws Exception
String endpoint
"http//mangal8080/axis/services/mypower"
Call call (Call) new Service().createCall(
) call.setTargetEndpointAddress(endpo
int) call.setOperationName("power")
Object value call.invoke(new
Object new Integer(2), new
Integer(5))
System.out.println(2""5 "" value)
35
How Does it Work?
  • The AXIS plugin is simply a Web application that
    resides in Tomcat (under webapps/)
  • The Servlet AxisServlet of this application is
    responsible for invoking services
  • All URLs of the form /services/ are mapped to
    the AxisServlet
  • Where is that written?

36
How Does it Work? (cont)
  • The wsdd file defines mappings between a Web
    service elements to a Java class elements
  • I.e., names, methods, etc.
  • The class AdminClient sends a request to the
    application to register the service based on the
    wsdd content
  • When a SOAP request arrives, the AxisServlet
    object parses the request and invokes the
    corresponding method of the class associated with
    the service URL

37
The Deployment Descriptor
ltdeployment xmlns"http//xml.apache.org/axis/wsdd
/" xmlnsjava"http//xml.apache.org/a
xis/wsdd/providers/java"gt ltservice
name"mypower" provider"javaRPC"gt
ltparameter name"className" value"myws.Power"/gt
ltparameter name"scope" value"application"
/gt ltparameter name"allowedMethods"
value""/gt lt/servicegt lt/deploymentgt
services.wsdd
38
The Scope of the Object
  • Request (the default) a new object is created
    for each request, the service instance is
    available for the duration of the request
    (regardless of forwarding)
  • Session a new object is created for each new
    session and the service instance is available for
    the entire session
  • Application a singleton shared service instance
    is used to serve all invocations

39
Undeploying a Service
ltundeployment xmlns"http//xml.apache.org/axis/ws
dd/"gt ltservice name"mypower"/gt lt/undeploymen
tgt
undeploy.wsdd
java org.apache.axis.client.AdminClient
-hlocalhost -p8080 undeploy.wsdd
40
Implementing Classes
  • The class that implements the Web service must be
    accessible to the Axis Servlet
  • Hence, this class should reside in a package in
  • CATALINA_BASE/webapps/axis/WEB-INF/classes/
  • Of course, all helper classes should be
    accessible to the Axis application too

41
The Service WSDL
  • Axis automatically provides a WSDL for each
    deployed service
  • To get the WSDL, use the service URL with the
    empty argument wsdl
  • http//localhost8080/axis/services/mypower?wsdl

42
Power-Service WSDL
lt?xml version"1.0" encoding"UTF-8"?gt ltwsdldefin
itions targetNamespace"http//inferno-02/axis/ser
vices/mypower" xmlns"http//schemas.xmlsoap.o
rg/wsdl/" xmlnsapachesoap"http//xml.apache.or
g/xml-soap" xmlnsimpl"http//localhost/axis/se
rvices/mypower" xmlnsintf"http//localhost/axi
s/services/mypower" xmlnssoapenc"http//schema
s.xmlsoap.org/soap/encoding/" xmlnswsdl"http/
/schemas.xmlsoap.org/wsdl/" xmlnswsdlsoap"htt
p//schemas.xmlsoap.org/wsdl/soap/"
xmlnsxsd"http//www.w3.org/2001/XMLSchema"gt
ltwsdlmessage name"powerResponse"gt
ltwsdlpart name"powerReturn" type"xsdint"/gt
lt/wsdlmessagegt ltwsdlmessage
name"powerRequest"gt ltwsdlpart name"a"
type"xsdint"/gt ltwsdlpart name"n"
type"xsdint"/gt lt/wsdlmessagegt
43
ltwsdlportType name"Power"gt ltwsdloperation
name"power" parameterOrder"a n"gt
ltwsdlinput message"implpowerRequest"
name"powerRequest"/gt ltwsdloutput
message"implpowerResponse" name"powerResponse"
/gt lt/wsdloperationgt lt/wsdlportTypegt
44
ltwsdlbinding name"mypowerSoapBinding"
type"implPower"gt ltwsdlsoapbinding
style"rpc" transport"http//schemas.x
mlsoap.org/soap/http"/gt ltwsdloperation
name"power"gt ltwsdlsoapoperation
soapAction""/gt ltwsdlinput
name"powerRequest"gt ltwsdlsoapbody
encodingStyle"http//schemas.xmlsoap.org/
soap/encoding/" namespace"http//myws"
use"encoded"/gt lt/wsdlinputgt
ltwsdloutput name"powerResponse"gt
ltwsdlsoapbody
encodingStyle"http//schemas.xmlsoap.org/soap/enc
oding/" namespace"http//loc
alhost/axis/services/mypower" use"encoded"/gt
lt/wsdloutputgt lt/wsdloperationgt lt/wsdl
bindinggt
45
ltwsdlservice name"PowerService"gt
ltwsdlport binding"implmypowerSoapBinding"
name"mypower"gt ltwsdlsoapaddress
location"http//inferno-028080/
axis/services/mypower"/gt lt/wsdlportgt
lt/wsdlservicegt lt/wsdldefinitionsgt
46
jws Files
  • There is a fast and easy way of crating a
    service
  • Create a Java class myClass.java
  • Rename your class to end with jws myClass.jws
  • Put the jws file directly under the directory
    CATALINA_BASE/webapps/axis/ (the applications
    root)
  • That is all. Now you can call the service!

47
Example a Calculator Service
public class SimpleCalculator public int
add(int i1, int i2) return i1 i2
public int subtract(int i1, int i2) return
i1 - i2
CATALINA_BASE/webapps/axis/SimpleCalculator.jws
Service URL http//serverport/axis/SimpleCalcula
tor.jws
48
Example a Calculator Service
public class CalculatorClient public static
void main(String argv)throws Exception
String endpoint
"http//localhost8080/axis/SimpleCalculator.jws"
Call call (Call) new
Service().createCall()
call.setTargetEndpointAddress(endpoint)
call.setOperationName("add") Object
value call.invoke(new Object new
Integer(4), new Integer(6))
System.out.println(value)
49
How does it Work?
  • On the first time the jws file is being called,
    it is compiled into a class
  • WEB-INF/jwsClasses/SimpleCalculator.class
  • Axis then considers the URL of the jws as one of
    a regular web service
  • Default configurations are assumed
  • Sounds like any other technology we know?

50
When not to Use jws Files
  • When you do not have the Java source code
  • When you dont want to expose your code
  • When you want to use custom type mappings
  • When you want to use other configuration options

51
Axis Type Mappings
52
Axis Type Mappings
  • Axis uses mappings between SOAP types and Java
    classes and primitive types
  • Serialization and deserialization are executed by
    objects of the interfaces Serializer and
    Deserializer (provided by Axis), respectively
  • For example, SimpleSerializer,
    SimpleDeserializer, DateDeserializer,
    ArraySerializer

Axis Type Mapping
53
Complex Type Example - Service Side
package myws import java.util. public class
VectorService public Vector
getAsVector(int array) Vector result
new Vector() for(int i0 iltarray.length
i) result.add("Number "
arrayi) return result
VectorService.java
54
Input in The WSDL
ltcomplexType name"ArrayOf_xsd_int"gt
ltcomplexContentgt ltrestriction
base"soapencArray"gt ltattribute
ref"soapencarrayType" wsdlarrayType"xsdint"
/gt lt/restrictiongt lt/complexContentgt lt/com
plexTypegt
ltwsdlmessage name"getAsVectorRequest"gt
ltwsdlpart name"array" type"implArrayOf_xsd_int
"/gt lt/wsdlmessagegt
55
Output in The WSDL
ltcomplexType name"Vector"gt ltsequencegt
ltelement maxOccurs"unbounded" minOccurs"0"
name"item" type"xsdanyType"/gt
lt/sequencegt lt/complexTypegt
ltwsdlmessage name"getAsVectorResponse"
ltwsdlpart name"getAsVectorReturn"
type"apachesoapVector"/gt lt/wsdlmes
sagegt
56
Complex Type Example - Client Side
import java.util. import org.apache.axis.client.
public class VectorClient public static
void main(String argv)throws Exception
String endpoint
"http//localhost7000/axis/services/tovector"
Call call (Call) new Service().createCall(
) call.setTargetEndpointAddress(endpoint)
call.setOperationName("getAsVector")
int array 3,7 Vector value
(Vector)call.invoke(new Object array)
printVector(value)
VectorClient.java
57
A Snapshot from the Request
ltgetAsVector soapenvencodingStyle"http//s
chemas.xmlsoap.org/soap/encoding/"gt ltarg0
xsitype"soapencArray"
soapencarrayType"xsdint2"
xmlnssoapenc"http//schemas.xmlsoap.org/soap/enc
oding/"gt ltitemgt3lt/itemgt
ltitemgt7lt/itemgt lt/arg0gt lt/getAsVectorgt
58
A Snapshot from the Response
ltgetAsVectorResponse soapenvencodingStyle"h
ttp//schemas.xmlsoap.org/soap/encoding/"gt
ltgetAsVectorReturn href"id0"/gt lt/getAsVectorResp
onsegt ltmultiRef id"id0" soapencroot"0"
soapenvencodingStyle"http//schemas.xmlsoap.org
/soap/encoding/" xsitype"ns1Vector"
xmlnssoapenc"http//schemas.xmlsoap.org/soap/en
coding/" xmlnsns1"http//xml.apache.org/x
ml-soap"gt ltitem xsitype"soapencstring"gtN
umber 3lt/itemgt ltitem xsitype"soapencstri
ng"gtNumber 7lt/itemgt lt/multiRefgt
59
Java Beans
  • Java Beans are simply objects of classes that
    follow some (natural) coding conventions
  • An empty constructor
  • A readable property has a matching getter
  • A writable property has a matching setter
  • A property has name and a type
  • The getter of property prop type getProp()
  • The setter of property prop void setProp(type)

60
An Example
package myws public class Person private
String firstName, lastName private int
personID public String getFirstName()
return firstName public void
setFirstName(String firstName)
this.firstName firstName public String
getLastName() return lastName public void
setLastName(String lastName)
this.lastName lastName public void
setId(int id) personID id public int
getId(int id) return personID
61
Using Beans in Web Services
  • A Web service can use beans in its definition
  • either in its input or output
  • For that, you need to tell the Axis engine that
    the corresponding object needs to be serialized
    as a bean
  • In this serialization, every accessible property
    (i.e., one that has a getter/setter) is specified
  • Where, in the file system, should the bean class
    reside?

62
A Service Example
package myws public class PersonService
public Person createPerson() Person
person new Person() person.setFirstName("
f") person.setLastName("l")
person.setId(2) return person
63
The WSDD
ltdeployment xmlns"http//xml.apache.org/axis/wsdd
/" xmlnsjava"http//xml.apache.org
/axis/wsdd/providers/java"gt ltservice
name"person" provider"javaRPC"gt
ltparameter name"className"
value"myws.PersonService"/gt
ltparameter name"scope" value"application"/gt
ltparameter name"allowedMethods" value""/gt
ltbeanMapping qname"nsperson"
xmlnsns"http//www.cs.huji.ac.il/dbi/xsd"
languageSpecificType"javamyws.Person"/gt
lt/servicegt lt/deploymentgt
64
Using Beans in Clients
  • To use a bean on the client side, you need to
    configure service to deserialize the SOAP type
    into a Java bean
  • For that, we use the class BeanDeserializer
    provided by Axis
  • Note that client and server beans need not be the
    same class
  • What should they have in common?

65
public class PersonClient public static void
main(String argv) throws Exception String
endpoint "http//localhost/axis/services/person"
Call call (Call) new Service().createCall()
call.setTargetEndpointAddress(endpoint)
call.setOperationName("createPerson") QName
qn new QName("http//www.cs.huji.ac.il/
dbi/xsd", "person") BeanDeserializerFactory
bdf new BeanDeserializerFactory(MyP
erson.class,qn) call.registerTypeMapping(MyPer
son.class, qn, null, bdf) MyPerson value
(MyPerson)call.invoke(new Object )
System.out.println(value)
Identical to Person
66
Exception Handling
67
Exception in Service Calls
  • Several problems can cause exception to be thrown
    when a service is being invoked
  • For example
  • the server cannot be connected
  • the server does not find the requested URL
  • the request is inappropriate (no such operation,
    invalid arguments, etc.)
  • the implementing service method has thrown an
    exception

68
Service Exception
  • When the implementing service throws an
    exception, a SOAP fault response is returned by
    the server
  • The client invocation method translates this
    response to a java.rmi.RemoteException after
    parsing the response

69
An Example
package myws public class ExceptionService
public void throwException() throws
IllegalStateException throw new
IllegalStateException ("I
only throw an exception!")
ltservice name"exception" provider"javaRPC"gt
ltparameter name"className"
value"myws.ExceptionService"/gt
ltparameter name"allowedMethods"
value""/gt lt/servicegt
services.wsdd
70
public class ExceptionClient public static
void main(String argv) String endpoint
"http//localhost80/axis/services/exception"
try Call call (Call) new
Service().createCall() call.setTargetEndpoin
tAddress(endpoint) call.setOperationName("th
rowException") Object value
call.invoke(new Object )
System.out.println(value)
catch(RemoteException exp)
System.err.println("WS invocation error "

exp.getMessage()) catch(Exception exp)
System.err.println("Error "
exp.getMessage())
71
A Snapshot from the Response
ltsoapenvBodygt ltsoapenvFaultgt
ltfaultcodegtsoapenvServer.userExceptionlt/faultcode
gt ltfaultstringgtjava.lang.IllegalStateExcept
ion I only throw an
exception!lt/faultstringgt ltdetailgt...lt/detail
gt lt/soapenvFaultgt lt/soapenvBodygt
Write a Comment
User Comments (0)
About PowerShow.com