...
Info | ||
---|---|---|
| ||
For new users, Web Services can be created in two ways.:
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. |
...
- 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 & Class that implement 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
...
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 | ||
---|---|---|
| ||
For new users, the terms that are most commonly used in Web Services are
|
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.Expand WEB-INF directory and add the following code to web.xml
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 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
- In Servers view, right click on the Apache Geronimo Server Runtime and select Add and Remove Projects
- In the popup dialog, select the jaxws-converter project, click Add, and then click Finish
- Wait for some time till the server status is changed to synchronized.
Testing
- The Web Service will now be deployed, and Geronimo will create the required .wsdl and other required artifacts.
- 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 - Now you should see the screen telling that this is Converter Web Service a message from Axis2 engine as below:
Info | ||
---|---|---|
| ||
You can also view the WSDL file generated by Geronimo based on the annotations specified by going to the following url |
Using Web Services Explorer in Eclipse
...
Info | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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
- Go to Eclipse and select Run->Launch the Web Services Explorer from the main menu.
- Web Services Explorer opens up as shown below:
- 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.
- 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.
- 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.
- Enter any argument and examine the status frame at the bottom to see the result.
This concludes our deploy and test section
...
This concludes our deploy and test section.
Info | ||
---|---|---|
| ||
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. |
...
title | How 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
- Right now our server status might be reporting that we need to republish.
- Right click on the Apache Geronimo Server Runtime present in the servers view and select Publish
- Wait for some time till the server status changes to Synchronized
Testing
- Right click the index.jsp present under WebContent directory of our project and select Run As->Run On Server
- In the popup, check the check box Always use this servr when running the project and then click Finish
- Now Eclipse will try to open the jsp in a web browser which shows you a form to enter amount in Dollars.
- Enter any amount and press submit, the jsp should display the result that is returned by the web service.
...
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
- Now Right click the ConverterClient.class and select Run As->Run as Java Application
- Now enter the amount in the console window of Eclipse
- The output will be shown which is retrieved by accessing the methods of Web service.
...