This tutorial will take you through the steps required in developing, deploying and testing a RESTful Web Service Client in Apache Geronimo for a web services which are already deployed on the server

We will be creating a Web based Client which can access the RESTful web service via GET and POST methods. You can easily create this client without any external tools except that of a server environment.

To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.

Details on installing Eclipse are provided in the Development environment section.

Deployed Web Service

This tutorial assumes that you have completed the Developing a simple RESTful Service tutorial. Here we will try to develop the client for the web service deployed in the above mentioned tutorial.

This tutorial will take you through the following steps:

2listpipe

Create a Dynamic Web Project to consume the Web Service

Developing the Web based Client

index.jspsolid RESTful Client

RESTful Client



Test via Post
Dollars:
]]>
post.jspsolid RESTful Client

RESTful Client

Test via Get

Enter full file Name:
]]>
ConverterHandler.javasolid 0) { out.println("
"); out.println("


"); String requestquery = "http://localhost:8080/jaxws-rest-converter/converter?amount=" + query; HttpClient client = new HttpClient(); GetMethod method = new GetMethod(requestquery); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } InputStream rstream = null; rstream = method.getResponseBodyAsStream(); Document queryresponse = null; try { queryresponse = DocumentBuilderFactory.newInstance() .newDocumentBuilder().parse(rstream); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); NodeList nodes = null; try { nodes = (NodeList) xPath.evaluate("/return", queryresponse, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } int nodeCount = nodes.getLength(); for (int i = 0; i < nodeCount; i++) { // Get each xpath expression as a string String rupees = null; String euros = null; try { rupees = (String) xPath.evaluate("dollarToRupeesResponse", nodes.item(i), XPathConstants.STRING); euros = (String) xPath.evaluate("rupeesToEurosResponse", nodes.item(i), XPathConstants.STRING); } catch (XPathExpressionException e) { e.printStackTrace(); } out.println(query + " Dollars equals to " + rupees + " Rupees"); out.println("
"); out.println(rupees + " Rupees equals to " + euros + " Euros"); } out.println("
"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Has to be Completed } } ]]>
Errors in Servlet

Here the servlet might notify that some imports are not resolved. We need to add two external jar files to resolve these errors.
The jar files that needed to be added in the build path are:
commons-codec-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\commons-codec\commons-codec\1.3\commons-codec-1.3.jar
commons-httpclient-3.0.1.jar - <GERONIMO_INSTALL_DIR>\repository\commons-httpclient\commons-httpclient\3.0.1\commons-httpclient-3.0.1.jar

This concludes the development section of our web based client.

Setting Up the Deployment Plan

geronimo-web.xmlsolid default jaxws-rest-converterclient 1.0 car commons-codec commons-codec 1.3 jar commons-httpclient commons-httpclient 3.0.1 jar /jaxws-rest-converterclient ]]>

Deploying and Testing the Web Client

Deploy

Testing

Test via Post

The code for testing the RESTful service via POST method hasn't been added to the ConverterHandler.
A sample post file might look like this

SampleRequest.xmlsolid ]]>

This completes the development of a simple RESTful client that consumes a RESTful Web Service that is already deployed on the server.