Joke Server example
Joke
Metaservice
Joke
Server
Joke
Client
meta
service
client
server
1 2 3 4 5 6Everything is XML over HTTP (REST).
Henning Niss SOAP and WSDLwith Java and Axis – p.2
SOAP, WSDL, UDDI
UDDI
registry
client
SOAP&WSDL
joke service
register service lookup WSDL SOAP req SOAP respSOAP and WSDL
with Java and Axis
Interactive web services, Course, Fall 2003
Henning Niss
IT University of Copenhagen
Web services with Axis
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);
server that plugs into Tomcat.
A Simple Joke Client (1)
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.
Henning Niss SOAP and WSDLwith Java and Axis – p.6
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>Henning Niss SOAP and WSDLwith Java and Axis – p.7
A Simple Joke Server (1)
interface JokeServer {void submit(String joke, String category);
String[] lookup(String category);
String[] categories();
}
Henning Niss SOAP and WSDLwith Java and Axis – p.4
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 (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/encoding/" namespace="urn:HenningsJokeServer"/> </wsdl:input> <wsdl:output name="lookupResponse"> <wsdlsoap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:HenningsJokeServer"/> </wsdl:output> </wsdl:operation> ... </wsdl:binding>Henning Niss SOAP and WSDLwith Java and Axis – p.10
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>Joke Server WSDL (2)
<wsdl:message name="lookupRequest"><wsdl:part name="category" type="xsd:string"/> </wsdl:message> <wsdl:message name="lookupResponse"> <wsdl:part name="lookupReturn" type="impl:ArrayOf_xsd_string"/> </wsdl:message> <wsdl:message name="submitRequest">
<wsdl:part name="joke" type="xsd:string"/> <wsdl:part name="category" type="xsd:string"/> </wsdl:message> <wsdl:message name="submitResponse" /> <wsdl:message name="categoriesRequest" /> <wsdl:message name="categoriesResponse"> <wsdl:part name="categoriesReturn" type="impl:ArrayOf_xsd_string"/> </wsdl:message>
Henning Niss SOAP and WSDLwith Java and Axis – p.8
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>
A Simple Joke Server (3)
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:HenningsJokeServer"
-p"jokeserver" "urn:HenningsJokeServer"
jokeserver.JokeServer
Henning Niss SOAP and WSDLwith Java and Axis – p.14
A Simple Joke Server (4)
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 Client (2)
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)
Henning Niss SOAP and WSDLwith Java and Axis – p.12
A Simple Joke Client (3)
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(); }
} }