...
- From Eclipse main menu, select File->New->Other
- In the New dialog, select Web->Dynamic Web Project and click Next
- Type jaxws-converter as the Project Name and click Next
- On the Project Facets page, the default selections as shown below are enough (Geronimo has an embedded Axis2 container already running) and click Next
- Make sure that the check box Generate Deployment Descriptor is selected and click Next
- On the Geronimo Deployment Page modify the Group Id to org.apache.geronimo.samples.jaxws and the Artifact Id to jaxws-converter.
- Click Finish
Add the POJO Interface and Class that
...
implements the Web Service
- In Project Explorer view, expand jaxws-converter project, right click on Java Resources: src and select New->Package
- Specify org.apache.geronimo.samples.jaxws as the package name and click Finish
- Right click on the new package and select New->Interface
- Name the interface as Converter and click Finish
- Add the following code to the Converter interface
Code Block title Converter.java borderStyle solid package org.apache.geronimo.samples.jaxws; import java.math.BigDecimal; import javax.jws.WebService; @WebService(name="ConverterPortType", targetNamespace = "http://jaxws.samples.geronimo.apache.org") public interface Converter { public BigDecimal dollarToRupees(BigDecimal dollars); public BigDecimal rupeesToEuro(BigDecimal rupees); }
- In Project Explorer view, right click on the new package and select New->Class
- Name the class as ConverterService, add org.apache.geronimo.samples.jaxws.Converter as Interface and click Finish
- Add the following code to the ConverterService class
Code Block title ConverterService.java borderStyle solid package org.apache.geronimo.samples.jaxws; import java.math.BigDecimal; import javax.jws.WebService; @WebService(serviceName = "Converter", portName="ConverterPort", endpointInterface = "org.apache.geronimo.samples.jaxws.Converter", targetNamespace = "http://jaxws.samples.geronimo.apache.org" ) public class ConverterService implements Converter { private BigDecimal rupeeRate = new BigDecimal("40.58"); private BigDecimal euroRate = new BigDecimal("0.018368"); public BigDecimal dollarToRupees(BigDecimal dollars) { BigDecimal result = dollars.multiply(rupeeRate); return result.setScale(2, BigDecimal.ROUND_UP); } public BigDecimal rupeesToEuro(BigDecimal rupees) { BigDecimal result = rupees.multiply(euroRate); return result.setScale(2, BigDecimal.ROUND_UP); } }
...
Code Block | ||||
---|---|---|---|---|
| ||||
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>jaxws-converter</display-name> <servlet> <display-name>Converter</display-name> <servlet-name>Converter</servlet-name> <servlet-class> org.apache.geronimo.samples.jaxws.ConverterService </servlet-class> </servlet> <servlet-mapping> <servlet-name>Converter</servlet-name> <url-pattern>/converter</url-pattern> </servlet-mapping> </web-app> |
Deploy the Web Service
...
- In Servers view, right click on Apache Geronimo Server Runtime and select Add and Remove Projects
- In the popup dialog, select jaxws-converter project, click Add, and then click Finish
The Web Service will now be deployed, and Geronimo will create the required .wsdl and other required artifacts. - Once the Web Service is deployed on to the server (Server Status says Synchronized), launch a browser and go to the following url:
http://localhost:8080/jaxws-converter/converter
You should see a message from Axis2 engine as below:
- You can also view the WSDL file generated by Geronimo by going to the following url:
http://localhost:8080/jaxws-converter/converter?wsdl
...
- From Eclipse's main menu, click Run->Launch the Web Services Explorer
- Web Services Explorer will open up as shown below:
- On the top right corner of this explorer, click on the icon for "WSDL Page". Under Navigation frame, you will now see WSDL Main (instead of UDDI Main). Click that WSDL Main link and the Actions frame would show a form as below:
- Enter the URL of your WSDL document i.e http://localhost:8080/jaxws-converter/converter?wsdl in the form and click Go. Now you can see the methods that are exposed by your Web Service.
- Clicking on any method would take you to a form asking for input parameters. If you don't see any field to enter input arguments click Add.
- Enter the input arguments and click Go. You can see the output of the Web Service in the Status frame at the bottom.
Developing a client
Now, we will focus on developing a client for our application. We will develop two types of clients to demonstrate how web services can be consumed in both cases.
- A Web based client
- POJO client
Web based Client
Now we will go through the steps involved in creating a client for consuming the web service just deployed.
Developing the Web based Client
...
Code Block | ||||
---|---|---|---|---|
| ||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Converter</title>
<meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type">
</head>
<body>
<center>
<h3>This from invokes a Web Service.</h3>
<br>
Please type an amount and click submit to see the result.
<form action="result.jsp">Amount(in Dollars): <input type="text"
name="amount"> <input type="submit" value="Submit"></form>
<br>
</center>
</body>
</html>
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.net.URL"%>
<%@page import="javax.xml.namespace.QName"%>
<%@page import="java.math.BigDecimal"%>
<%@ page import="javax.xml.ws.Service"%>
<%@ page import="org.apache.geronimo.samples.jaxws.Converter"%>
<html>
<head>
<title>Converter</title>
<meta content="text/html; CHARSET=iso-8859-1" http-equiv="Content-Type">
</head>
<body>
<%
String amount = request.getParameter("amount");
if (amount != null && amount.trim().length() > 0) {
out.println("<center>");
try {
BigDecimal dollars = new BigDecimal(amount);
URL url = new URL(
"http://localhost:8080/jaxws-converter/converter?wsdl");
QName qname = new QName(
"http://jaxws.samples.geronimo.apache.org",
"Converter");
Service service = Service.create(url, qname);
Converter conv = (Converter) service
.getPort(Converter.class);
BigDecimal rupees = conv.dollarToRupees(dollars);
BigDecimal euros = conv.rupeesToEuro(rupees);
out.println(dollars + " Dollars equals to " + rupees
+ " Rupees");
out.println("<br>");
out.println(rupees + " Rupees equals to " + euros
+ " Euros");
} catch (Exception e) {
out.println("Error: " + e.getMessage());
}
out.println("<center>");
}
%>
</body>
</html>
|
This concludes the development section of our web based client.
Deploying and Testing the Web Client
Deploy
...
Testing
...
Info | ||
---|---|---|
| ||
Note that we accessed the webservice in the jsp by creating a service, and then retrieving the Converter port from the WSDL document. |
POJO Client
Here we will have a look on how to create a plain old java client for consuming the web service.
Developing the Client
...
Code Block | ||||
---|---|---|---|---|
| ||||
package org.apache.geronimo.samples.jaxws;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class ConverterClient {
public static void main(String args[]) {
try {
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a amount to convert:");
String number = new String();
number = dataIn.readLine();
BigDecimal amount = new BigDecimal(number);
URL url = new URL("http://localhost:8080/jaxws-converter/converter?wsdl");
QName qname = new QName("http://jaxws.samples.geronimo.apache.org","Converter");
Service service = Service.create(url, qname);
Converter conv =(Converter)service.getPort(Converter.class);
BigDecimal result = conv.dollarToRupees(amount);
System.out.println(amount+" dollars equals to "+result+" rupees");
BigDecimal result1 = conv.rupeesToEuro(result);
System.out.println(result+" rupees equals to "+result1+" euros");
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
This completes the development section of our POJO client
Adding necessary jar files to the class path
Note | ||
---|---|---|
| ||
Now our Java project might show many errors and even if we resolve the errors the application may not run properly. This is due to the missing jar files required to access web service in class path |
...
Info | ||
---|---|---|
| ||
The version numbers may differ slightly. It wont matter much. |
...
Panel | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
axis2-adb-1.3.jar - <GERONIMO_INSTALL_DIR>/repository/org/apache/axis2/axis2-adb/1.3/axis2-adb-1.3.jar |
This completes the adding external JARs to the project
Tip | ||
---|---|---|
| ||
If you are running Java 6 you don't need to worry about the external JARs as they are all inbuilt into JRE 6. |
Testing
...
- .
This completes the process of deploying the web services in Geronimo and testing them. You can also refer to the following tutorial to develop a actual client for a web service that is deployed already on server Developing Client Applications for a JAX-WS Web Service