EGEE Introduction to Webservices Course Practicle ------------------------------------------------- This practical will show you how to write a client for a pre-defined webservice, in this case the Barns and Noble book query service. Step 0) Create a directory for you to work in. Call it bnquote, and make it in your user directory. Step 1) WSDL Get the WSDL from http://www.xmethods.com/ or, if that doesn't work there is a local copy here: /opt/webservices_course/BNQuoteService.wsdl Step 2) Generate the glue components First, create a file called config.xml, and add the following lines to it: This is the configuration file for the wscompile utility that will generate all of the glue code. The file tells wscompile which WSDL file to use, and what package name we would like the glue code placed in. Next, we run the wscompile utility itself: wscompile -gen:client -keep -d . config.xml This gets wscompile to generate the client code (-gen:client), keep the generated source code so we can examine it later (-keep), work in the current directory (-d .), using the configuration described in config.xml. This will create a bnquote directory, which will contain a number of .class and .java files that allow us to talk to the webservice. Step 3) Write the client Create a new file called bnclient.java in ~/bnquote/ First, we must import the various RPC libraries and glue code that we have just generated: // Standard libraries import javax.xml.rpc.Service; import javax.xml.rpc.Stub; // Our glue code import bnquote.BNQuoteService_Impl; import bnquote.BNQuotePortType; Next, we create our client class and give it two methods: public class bnclient { public static float getPrice(String url, String isbn) throws Exception { } public static void main(String[] args) { } } Now, we'll write the getPrice function so that it calls a remote function via webservices. Add the following to the getPrice function: BNQuoteService_Impl service = new BNQuoteService_Impl(); BNQuotePortType port = service.getBNQuotePort(); The BNQuoteService_Impl class can be seen as the "class" of the webservice, and BNQuotePortType can be seen as an "instance" of that class. Now we call the remote "getPrice" method, as defined in our WSDL, and return it to our caller: float value = port.getPrice(isbn); return (value); And that is all that our getPrice client method needs. Now, we'll write some code in our "main" method so that it calls our getPrice method: try{ float price = getprice("http://services.xmethods.net:80/soap/servlet/rpcrouter", "0596003994"); System.out.println(price); } catch (Exception e) {e.printStackTrace();} That should now be all of the code that is required. Compile the code with: javac bnclient.java Step 4) Run the Client Try the client out with: java bnclient If the client seems to hang, it could be because the remote service is down (welcome to the world of webservices! ;-)