You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

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.

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 5.0+ (J2SE 1.5)
  • Eclipse 3.3.1.1 (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:

This tutorial will take you through the following steps:

Setting Up Eclipse for Application Development

  1. Create a Dynamic Web Project
    • Select File->New->Project


      Unable to render embedded object: File (1.gif) not found.


    • In the popup window, Select Web->Dynamic Web Project category and click Next


      Unable to render embedded object: File (2.gif) not found.


    • Type jaxws-converter as the Project Name and click Next..


      Unable to render embedded object: File (3.gif) not found.


    • The default options should work in the case of Geronimo as it has axis2 web container already running, click Next


      Unable to render embedded object: File (4.gif) not found.


    • Make sure that the check box Generate Deployment Descriptor is selected and click Next


      Unable to render embedded object: File (5.gif) not found.


    • Modify the Group Id to org.apache.geronimo.samples.jaxws and the artifact id to jaxws-converter.


      Unable to render embedded object: File (6.gif) not found.


    • Click Finish

This completes the configuration of Eclipse for application development.

Creating the Web Services Implementation code

To deploy the Converter service we are going to create a POJO class and expose it as a servlet. The steps required are:

  1. Right click on JavaRsources:src and select New->Package


    Unable to render embedded object: File (7.gif) not found.


  2. Name the package to org.apache.geronimo.samples.jaxws and click Finish


    Unable to render embedded object: File (8.gif) not found.


  3. Right click on the new package and select New->Class


    Unable to render embedded object: File (9.gif) not found.


  4. Name the class as Converter and click Finish


    Unable to render embedded object: File (10.gif) not found.


  5. Add the following code to the Converter class
    Bar.java
    
    package org.apache.geronimo.samples.jaxws;
    
    import javax.jws.WebService;
    
    @WebService(serviceName = "Converter",
                portName="ConverterPort",
                targetNamespace = "http://jaxws.samples.geronimo.apache.org"
                )
    public class Converter
    {
      public float celsiusToFarenheit ( float celsius )
      {
        return (celsius * 9 / 5) + 32;
      }
    
      public float farenheitToCelsius ( float farenheit )
      {
        return (farenheit - 32) * 5 / 9;
      }
    }
    
    

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.

Setting Up the Deployment Descriptor and Deployment Plan

For the client applications to enquire about the services provided we need to create a WSDL file which provides the mapping between services exposed by web service and functions in the Java class

Some Typical Web Services Terminology

For new users, the terms that are most commonly used in Web Services are

  • Web Services Description Language (WSDL) - The WSDL defines services as collections of network endpoints, or ports
  • Simple Object Access Protocol (SOAP) - SOAP is a protocol for exchanging XML-based messages over computer networks, normally using HTTP/HTTPS.
  • Universal Description, Discovery and Integration (UDDI) - UDDI is a directory service where businesses can register and search for Web services
  1. Expand WEB-INF directory and add the following code to web.xml
    
    <?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>Converter</display-name>
    	<servlet>
    		<display-name>Converter</display-name>
    		<servlet-name>Converter</servlet-name>
    		<servlet-class>
    			org.apache.geronimo.samples.jaxws.Converter
    		</servlet-class>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>Converter</servlet-name>
    		<url-pattern>/converter</url-pattern>
    	</servlet-mapping>
    
    </web-app>
    
    

This completes the setting up of Deployment descriptor and Deployment Plan.

Deploy and Test the Web Service

Deploy

  1. Right click on the Apache Geronimo Server Runtime present in the servers view and select Add or Remove Projects


    Unable to render embedded object: File (11.gif) not found.


  2. In the popup dialog, select the jaxws-converter project and click Add


    Unable to render embedded object: File (12.gif) not found.


  3. Make sure that jaxws-converter is in the configured projects list and then click Finish


    Unable to render embedded object: File (13.gif) not found.


  4. Wait for some time till the server status is changed to synchronized.

Testing

  1. Once the application is deployed on to the server, Launch a browser and go to the following url.
    http://localhost:8080/jaxws-converter/converter
  2. Now you should see the screen telling that this is Converter Web Service


    Unable to render embedded object: File (14.gif) not found.


    WSDL File

    You can also view the WSDL file generated by Geronimo based on the annotations specified by going to the following url
    http://localhost:8080/jaxws-converter/converter?wsdl

Using Web Services Explorer in Eclipse
  1. Go to Eclipse and Run->Launch the Web Services Explorer from the main menu.


    Unable to render embedded object: File (15.gif) not found.


  2. Click the WSDL icon to go to the WSDL main page.


    Unable to render embedded object: File (16.gif) not found.


  3. Click the WSDL main link to enter the URL of WSDL document.


    Unable to render embedded object: File (17.gif) not found.


  4. Enter the URL of our WSDL document i.e http://localhost:8080/jaxws-converter/converter?wsdl in the form and click Go


    Unable to render embedded object: File (18.gif) not found.


  5. Now you can see the methods that are exposed by our Web Service.


    Unable to render embedded object: File (19.gif) not found.


  6. Clicking on any method takes us to a page with form asking for input parameters.


    Unable to render embedded object: File (20.gif) not found.


  7. Enter any argument and examine the status frame at the bottom to see the result.


    Unable to render embedded object: File (21.gif) not found.


Unknown macro: {dynamictasklist}
  • No labels