Joke Server example SOAP and WSDL with Java and Axis Interactive web services, Course, Fall 2003 Henning Niss Joke Metaservice Joke Server Joke Client 3 meta service 2 IT University of Copenhagen client 4 5 1 server 6 Everything is XML over HTTP (REST). Henning Niss SOAP and WSDLwith Java and Axis p.2 Web services with Axis SOAP, WSDL, UDDI Today: How to invoke and deploy web services with Apache Axis. Axis is a SOAP engine with extensive support for WSDL. a framework for constructing SOAP processors (clients, servers, gateways, etc); support for WSDL; tools to generate Java classes from WSDL (and WSDL from Java); client lookup UDDI registry WSDL SOAP req SOAP resp register service SOAP&WSDL joke service server that plugs into Tomcat. Web: http://xml.apache.org/axis/. enning Niss SOAP and WSDLwith Java and Axis p.1 Henning Niss SOAP and WSDLwith Java and Axis p.3
A Simple Joke Server (1) A Simple Joke Client (1) interface JokeServer { void submit(string joke, String category); String[] lookup(string category); Example only: a client that uploads one joke to a joke server and immediate requests the jokes in the specified category. Need to know the WSDL-description of the server. String[] categories(); enning Niss SOAP and WSDLwith Java and Axis p.4 Henning Niss SOAP and WSDLwith Java and Axis p.6 A Simple Joke Server (2) public class JokeServerImpl implements JokeServer { Map jokes = new TreeMap(); // maps categories to lists of jokes public void submit(string joke, String category) { LinkedList catjokes = (LinkedList)jokes.get(category); if(catjokes==null) catjokes = new LinkedList(); catjokes.addlast(joke); jokes.put(category, catjokes); public String[] lookup(string category) { List catjokes = (List)jokes.get(category); String[] res = new String[catjokes.size()]; return catjokes.toarray(res); public String[] categories() { String[] res = new String[jokes.size()]; return jokes.keyset().toarray(res); Joke Server WSDL (1) <?xml version="1.0" encoding="utf-8"?> <wsdl:definitions targetnamespace="urn:henningsjokeserver".../> <wsdl:types> <schema xmlns="http://www.w3.org/2001/xmlschema" targetnamespace="urn:henningsjokeserver"> <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/ <complextype name="arrayof_xsd_string"> <complexcontent> <restriction base="soapenc:array"> <attribute ref="soapenc:arraytype" wsdl:arraytype="xsd:string[]"/> </restriction> </complexcontent> </complextype> </schema> </wsdl:types> enning Niss SOAP and WSDLwith Java and Axis p.5 Henning Niss SOAP and WSDLwith Java and Axis p.7
Joke Server WSDL (2) <wsdl:message name="lookuprequest"> <wsdl:part name="category" type="xsd:string"/> <wsdl:message name="lookupresponse"> <wsdl:part name="lookupreturn" type="impl:arrayof_xsd_string"/> <wsdl:message name="submitrequest"> <wsdl:part name="joke" type="xsd:string"/> <wsdl:part name="category" type="xsd:string"/> <wsdl:message name="submitresponse" /> <wsdl:message name="categoriesrequest" /> <wsdl:message name="categoriesresponse"> <wsdl:part name="categoriesreturn" type="impl:arrayof_xsd_string"/> Joke Server WSDL (4) <wsdl:binding name="jokeserversoapbinding" type="impl:jokeserver"> <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="lookup"> <wsdlsoap:operation soapaction=""/> <wsdl:input name="lookuprequest"> <wsdlsoap:body use="encoded" encodingstyle="http://schemas.xmlsoap.org/soap/enc namespace="urn:henningsjokeserver"/> </wsdl:input> <wsdl:output name="lookupresponse"> <wsdlsoap:body use="encoded" encodingstyle="http://schemas.xmlsoap.org/soap/enc namespace="urn:henningsjokeserver"/> </wsdl:output> </wsdl:operation>... </wsdl:binding> enning Niss SOAP and WSDLwith Java and Axis p.8 Henning Niss SOAP and WSDLwith Java and Axis p.10 Joke Server WSDL (3) <wsdl:porttype name="jokeserver"> <wsdl:operation name="lookup" parameterorder="category"> <wsdl:input name="lookuprequest" message="impl:lookuprequest"/> <wsdl:output name="lookupresponse" message="impl:lookupresponse"/> </wsdl:operation>... </wsdl:porttype> Joke Server WSDL (5) <wsdl:service name="jokeserverservice"> <wsdl:port name="jokeserver" binding="impl:jokeserversoapbinding"> <wsdlsoap:address location="http://localhost:8180/axis/services/jokeserver"/ </wsdl:port> </wsdl:service> enning Niss SOAP and WSDLwith Java and Axis p.9 Henning Niss SOAP and WSDLwith Java and Axis p.11
A Simple Joke Client (2) A Simple Joke Server (3) How to get from the WSDL description to code? Tool support! The WSDL2Java tool generates stubs for invoking the service. java org.apache.axis.wsdl.wsdl2java jokeserver.wsdl Generated classes: JokeServer.java (interface) JokeServerService.java (service interface) JokeServerServiceLocator.java (locate service) JokeServerSoapBindingStub.java (stub for talking to service) Tool support to generate a WSDL description based on the server interface. The Java2WSDL tool generates the WSDL description automatically. java org.apache.axis.wsdl.java2wsdl -o jokeserver.wsdl -l "http://localhost:8180/axis/services/jokeserver" -i jokeserver.jokeserverimpl -n "urn:henningsjokeserv -p"jokeserver" "urn:henningsjokeserver" jokeserver.jokeserver enning Niss SOAP and WSDLwith Java and Axis p.12 Henning Niss SOAP and WSDLwith Java and Axis p.14 A Simple Joke Client (3) A Simple Joke Server (4) import HenningsJokeServer.*; public class JokeClient { static final String CATEGORY = "languages"; static final String JOKE = "If it wasn t for C, we d be using BASI, PASAL and OBOL."; public static void main(string[] args) { try { JokeServerService service = new JokeServerServiceLocator(); JokeServer jokes = service.getjokeserver(); jokes.submit(joke,category); String[] res = jokes.lookup(category); for(int i=0; i<res.length; i++) System.out.println(res[i]); catch (Exception e) { e.printstacktrace(); enning Niss SOAP and WSDLwith Java and Axis p.13 Generating (SOAP) skeletons using the WSDL2Java tool. java org.apache.axis.wsdl.wsdl2java -o. -s -S true -Nurn:HenningsJokeServer -d Application jokeserver Generated classes: JokeServer.java (interface) JokeServerService.java (service interface) JokeServerServiceLocator.java (service locator) JokeServerSoapBindingImpl.java (template) JokeServerSoapBindingSkeleton.java (SOAP skeleton) JokeServerSoapBindingStub.java (SOAP stub) Henning Niss SOAP and WSDLwith Java and Axis p.15
A Simple Joke Server (5) Deploying a web service is a matter of telling the Axis implementation about it. Invoking WSDL2Java in server-side mode also generates a deployment descriptor deploy.wsdd. java org.apache.axis.client.adminclient -p8180 deploy.wsdd We can see the services deployed: java org.apache.axis.client.adminclient -p8180 list enning Niss SOAP and WSDLwith Java and Axis p.16