This tutorial will walk you through building and developing the jax-ws calculator that ships with the samples package for Geronimo. After completing this tutorial, you should be able to understand how to build simple web services with Apache Geronimo 2.1 using the Eclipse development environment
To run this tutorial, as a minimum you will be required to have installed the following prerequisite software.
- Sun JDK 5.0+ (J2SE 1.5)
- Eclipse 188.8.131.52 (Eclipse Classic package of Europa distribution), which is platform specific
- Web Tools Platform (WTP) 2.0.1
- Data Tools Platform (DTP) 1.5.1
- Eclipse Modeling Framework (EMF) 2.3.1
- Graphical Editing Framework (GEF) 3.3.1
Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:
The application development will take you through the following
Setting Eclipse for Application Development
- Create a Dynamic Web Project
- Select File -> New, select Other...
- In the popup window select Web->Dynamic Web Project category and click NEXT.
- Type in jaxws-calculator as the Project name: and select Next
- On the next screen check the box for Axis2 Web Services
- Make sure Generate Deployment Descriptor is selected and click Next.
- Modify the Group Id to: org.apache.geronimo.samples.jws and Artifact Id: to Calculator.
Group ID: A name that identifies the group of a module. The default value is default.
Artifact ID: A name of the module in a group. The default value is <blank>.
Version: The version of module. The default value is 1.0
Type: The type of module, such as system module (car), jar module (jar), web
archive (war), enterprise archive (ear), and so on. The default value is car
A module is stored inside a repository as repository/groupId/artifactId/version/artifactIdversion.
type. This repository structure gives a more standardized and easily maintainable
structure to Geronimo applications.
- Select Finish.
Depending upon your environment, you may see the error message "Failed while installing Axis2 Web Services Core 1.1". Do not worry. We will just have to manually point Axis2 to the installation directory of Apache Geronimo to fix this error.
- AXIS2 Configuration
- Select Window->Preferences.
- Select Web Services->Axis2 Preferences.
- Under Axis2 Runtime tab, browse to the installation root of Apache Geronimo. Select Ok.
This completes the configuration of Eclipse for application development.
Creating the Web Services Implementation code
To implement the Calculator we are going to create a package org.apache.geronimo.samples.jws. This package will contain 2 classes. A Calculator Interface, and CalculatorService which implements the Calculator interface. Lets go step by step with creating the package, interface class and implementation class.
- Right click on Java Resources: src and select New->Package.
- Name the package as org.apache.geronimo.samples.jws. Select Finish.
- Right click on the new package and Select New->Interface.
- Name the interface as Calculator. Select Finish.
- Add the following code to the Calculator interface class
- Right click on the package org.apache.geronimo.samples.jws and select
- Name the class CalculatorService
- Accept all the defaults and Select Finish.
- Add the following code to CalculatorService.class
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 suggests the user defined name for 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 donot 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.
This completes the development of the Web Service Implementation code.
Generating the wsdl for the web service
Geronimo provides a new jaxws-tools.bat which helps in generating the WSDL file from service endpoint interface.
- Export the source files to a jar as shown in the figure.
- Open a command prompt and point it to the bin directory of server installation.
- Run gsh. This will start the Gshell.
- Run the command
In this command -classpath is used to set source.jar(exported from eclipse) in the classpath, -d defines the location where all the generated artifacts will be placed, -wsdl:soap1.1 suggests a wsdl generation following soap1.1 protocol, org.apache.geronimo.samples.jws.CalculatorService is the SEI used to generate the WSDL.
- Once the above command is run Calculator_schema1.xsd and Calculator.wsdl will be generated at C:/wsdl.
- Rename Calculator.wsdl as CalculatorService.wsdl and add both the generated files above to WEB-INF directory of the project.
To learn more about GShell refer Gshell section in user guide. To know about more options associated with jaxws. Run jaxws/wsgen -help or jaxws/wsimport -help from gshell.
Why are we using gsh if we already have a jaxws-tools.bat
This is because there are some issues involved with jaxws-tools.bat in AG 2.1 so we are using a gshell to call the web services generator.
Developing a web client for Calculator
This section will take you through the creation of two jsp's index.jsp and result.jsp.index.jsp will prompt the
user to enter two whole number values to add together. After submitting the form, the action will be forwarded to result.jsp. Result.jsp will call the Calculator add Web Service.
- Right Click on the Web Content folder and Select New->JSP.
- Name the jsp as index.jsp and Select Finish.
- Add the following code to index.jsp
- Right Click on the Web Content folder and Select New->JSP.
- Name the jsp as result.jsp and Select Finish.
- Add the following code to jsp.
This finishes the development of Web client.
Setting up the Deployment Descriptor and Deployment Plan
- Expand WEB-INF/web.xml and add the following code
- Similarly double click geronimo-web.xml and add the following code.
Deploy and Run
- Right click on jaxws-calculator project. Select Run As->Run On Server.
- On the next window select Apache Geronimo V2.1 and Select Finish.
- Once the application is deployed on the server. Launch a browser and run the following URL http://localhost:8080/jaxws-calculator-1.0/index.jsp.
- Give the values as 5,7. Select =.
- The result is 12 as shown in the figure