This tutorial will take you through the steps required in developing, deploying and testing a Web Service in Apache Geronimo. After completing this tutorial you should be able to understand how to develop simple JAX-WS compliant web services in Apache Geronimo using Eclipse development environment.
This application has a Java class which contains two functions, one which converts amount in Dollars to Rupees and the other which converts Rupees to Euros. We will expose these two methods as the services provided by our deployed Web Service.
We will also test the deployed web service by using the Web Services Explorer in Eclipse
Types of Web Services
For new users, Web Services can be created in two ways:
- Bottom Up Web Service - creating web services from Java classes.
- Top Down Web Service - creating web services from WSDL document.
This tutorial will help you in creating a Bottom Up Web Service from a Java class which will be exposed as a Servlet to the client applications.
To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.
- Sun JDK 6.0+ (J2SE 1.6)
- Eclipse IDE for Java EE Developers, which is platform specific
- Apache Geronimo Eclipse Plugin 2.1.x
- Apache Geronimo Server 2.1.x
Geronimo version 2.1.x, Java 1.5 runtime, and Eclipse Ganymede are used is used in this tutorial but other versions can be used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Europa)
Details on installing eclipse are provided in the Development environment section. This tutorial will take you through the following steps:
Create a Dynamic Web Project to host the Web Service
- 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.
- 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:
Converter.java
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); }
- Right-click on the new package and select New --> Class.
- Name the class as ConverterService, add org.apache.geronimo.samples.jaxws.Converter as the interface and click Finish.
- Add the following code to the ConverterService class:
ConverterService.java
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); } }
- Let us try to understand each annotation:
- @WebService - this annotation can be used with a Java class as well as with interface. In our case we used it with both interface as well as the POJO. This annotation declares the POJO as a WebService. @WebService annotation is utilized in generating the WSDL file.
- serviceName is same as the WSDL element service
- name is same as the WSDL element <portType name>
- endpointInterface specified the class name of the Service Endpoint Interface (SEI).
- portName is the element portName
- targetNamespace is the XML namespace of the WSDL and some of the XML elements generated from the WebService
- @WebMethod - this annotation is applied to a method to expose it as a WebService method. In case you have multiple methods you can use this annotation to selectively expose methods as WebService method. If you don't use this annotation all the public methods will be exposed as WebService.
- @WebParam - this annotation is used along with @WebMethod annotation to define the WebService. It is used to customize parameter used in the message part of the WSDL.
Expose the Web Service as a Servlet in web.xml
- In Project Explorer view, double-click on jaxws-converter --> WebContent --> WEB-INF --> web.xml to open it in an editor and add the following <servlet> and <servlet-mapping> elements inside it.
<?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. Start the Geronimo server if it is not already running. 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
Generated files by Geronimo
Geronimo processes the annotations in source files and automatically generates the required artifacts to deploy the Web Service.
You can see the Geronimo created files in the directory <INSTALL_DIR>/repository/org/apache/geronimo/samples/jaxws/jaxws-converter/1.0/jaxws-converter-1.0.car/. At this location you can find a new directory which Geronimo created for deploying the web service which contains the WSDL file and other required stubs.
How does Geronimo create the WSDL and other required files ??
Geronimo has a built-in plug-in named jaxws-tools which provides tools for generating WSDL and other necessary files used in JAX-WS web services. The plug-in relies on Sun's wsgen and wsimport tools to generate the web services artifacts. Please see wsgen or wsimport documentation for more information.
Test the Web Service using Eclipse Web Services Explorer
- From Eclipse's main menu, click Run --> Launch the Web Services Explorer.
- The 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 the Navigation frame, you will now see WSDL Main (instead of UDDI Main). Click that WSDL Main link and the Actions frame will 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 will 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.
This completes the process of deploying the web service to Geronimo and testing it. You can also refer to the following tutorial to develop a actual client for a web service that is already deployed on Geronimo Developing Clients for a JAX-WS Web Service.