Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info
titleTypes 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 Servlet to the client applications.

...

  1. From Eclipse main menu, select File->New->Other
  2. In the New dialog, select Web->Dynamic Web Project and click Next
  3. Type jaxws-converter as the Project Name and click Next
  4. On the Project Facets page, the default selections as shown below are enough (Geronimo has an embedded Axis2 container already running) and click Next
  5. Make sure that the check box Generate Deployment Descriptor is selected and click Next
  6. On the Geronimo Deployment Page modify the Group Id to org.apache.geronimo.samples.jaxws and the Artifact Id to jaxws-converter.
  7. Click Finish

...

Add the POJO Interface & Class that implement the Web Service

  1. In Project Explorer view, expand jaxws-converter project, right click on Java Resources: src and select New->Package
  2. Specify org.apache.geronimo.samples.jaxws as the package name and click Finish
  3. Right click on the new package and select New->Interface
  4. Name the interface as Converter and click Finish
  5. Add the following code to the Converter interface

...

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.

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

Info
titleSome 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

Expose the Web Service as a Servlet in web.xml

  1. 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.Expand WEB-INF directory and add the following code to web.xml
Code Block
xml
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>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 and let Geronimo create the required .wsdl & other artifacts

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

Deploy and Test the Web Service

Now, we will look into the steps involved in deploying and testing our web service without any clients.

Deploy

  1. In Servers view, right click on the Apache Geronimo Server Runtime and select Add and Remove Projects
  2. In the popup dialog, select the jaxws-converter project, click Add, and then click Finish
  3. Wait for some time till the server status is changed to synchronized.

Testing

  1. The Web Service will now be deployed, and Geronimo will create the required .wsdl and other required artifacts.
  2. Once the Web Service Once the application is deployed on to the server , Launch (Server Status says Synchronized), launch a browser and go to the following url.:
    http://localhost:8080/jaxws-converter/converter
  3. Now you should see the screen telling that this is Converter Web Service a message from Axis2 engine as below:
Info
titleWSDL 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

...

Info
titleGenerated files by Geronimo

Geronimo processes the annotations in our 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.

Tip
titleHow Geronimo creates the WSDL and other required files?

Geronimo has an in-built plug-in by name jaxws-builder which helps in creating the WSDL and other required files to deploy the web service by using the jaxws-tools provided by Sun

Try looking into source of jaxws-builder to better understand how the necessary files are getting created.

Test the Web Service using Eclipse's Web Services Explorer

  1. Go to Eclipse and select Run->Launch the Web Services Explorer from the main menu.
  2. Web Services Explorer opens up as shown below:


    Image Added


  3. On the top right corner of this explorer, click on the icon for "WSDL Page". Under Navigation section, you will now see WSDL Main. Click that WSDL Main link to enter the URL of WSDL document.


    Image Added


  4. Enter the URL of our 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 our Web Service.


    Image Added


  5. Clicking on any method takes us to a page with form asking for input parameters.If you dont see any field to enter input arguments click Add.


    Image Added


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


    Image Added


This concludes our deploy and test section

...

This concludes our deploy and test section.

Info
titleGenerated files by Geronimo

For our convenience Geronimo automatically generates the required files to deploy the Web Services. The WSDL file is created by Geronimo after processing the annotations in our source file.

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.

...

titleHow Geronimo creates the WSDL and other required files?

Geronimo has a inbuilt plugin names jaxws-builder which helps in creating the WSDL and other required files to deploy the web service by using the jaxws-tools provided by Sun

...

.

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.

...

Deploying and Testing the Web Client

Deploy

  1. Right now our server status might be reporting that we need to republish.





  2. Right click on the Apache Geronimo Server Runtime present in the servers view and select Publish





  3. Wait for some time till the server status changes to Synchronized

Testing

  1. Right click the index.jsp present under WebContent directory of our project and select Run As->Run On Server





  2. In the popup, check the check box Always use this servr when running the project and then click Finish





  3. Now Eclipse will try to open the jsp in a web browser which shows you a form to enter amount in Dollars.





  4. Enter any amount and press submit, the jsp should display the result that is returned by the web service.





...

Tip
titleJRE 6

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

  1. Now Right click the ConverterClient.class and select Run As->Run as Java Application





  2. Now enter the amount in the console window of Eclipse
  3. The output will be shown which is retrieved by accessing the methods of Web service.





...