From scm-return-29866-apmail-geronimo-scm-archive=geronimo.apache.org@geronimo.apache.org Fri Jul 04 11:09:08 2008 Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 17646 invoked from network); 4 Jul 2008 11:09:06 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Jul 2008 11:09:06 -0000 Received: (qmail 57891 invoked by uid 500); 4 Jul 2008 11:09:07 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 57850 invoked by uid 500); 4 Jul 2008 11:09:06 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 57841 invoked by uid 99); 4 Jul 2008 11:09:06 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Jul 2008 04:09:06 -0700 X-ASF-Spam-Status: No, hits=-1996.1 required=10.0 tests=ALL_TRUSTED,HTML_MESSAGE,MIME_HTML_ONLY,OBFUSCATING_COMMENT X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Jul 2008 11:08:14 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 1720A234C154 for ; Fri, 4 Jul 2008 04:08:00 -0700 (PDT) Message-ID: <2015620848.1215169680026.JavaMail.www-data@brutus> Date: Fri, 4 Jul 2008 04:08:00 -0700 (PDT) From: confluence@apache.org To: scm@geronimo.apache.org Subject: [CONF] Apache Geronimo v2.1: Developing Clients for a JAX-WS Web Service (page edited) MIME-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org

Developing Clients for a JAX-WS W= eb Service has been edited by Sainath Chowdary (Jul 04, 2008).

=20

= (View changes)

Content:

This tutorial will take you through the steps required in developing, de= ploying and testing a Web Service Client in Apache Geronimo for a web servi= ces which are already deployed on the server

This tutorial will take you through the steps involved in creating a Web= based Client and a POJO Client for a JAX-WS web service. Here we will only= be needing the WSDL file of the web service deployed for creating the clie= nt.

3D""Types of Web Services

For new users, Web Services can be created in two ways:

    =09
  • Bottom Up Web Service - creating web services from Java classes. =09
  • Top Down Web Service - creating web services from WSDL document.

This tutorial will help you in creating a Top Down Web Service from WSDL= document which is already deployed on server.

To run this tutorial, as a minimum you will be required to have installe= d the following prerequisite software.

    =09
  • Sun JDK 5.0+ (J2SE 1.5)
  • =09
  • Apache Geronimo 2.x
  • =09
  • Eclipse IDE for Java EE Developers - Europa release
  • =09
  • Geronimo Eclipse Plug-in 2.x

Details on installing eclipse are provided in the Development environment section.

3D""

This tutorial assumes that you have completed the Developing a JAX-WS POJO Web Service t= utorial. Here we will try to develop the client for the web service deploye= d in the above mentioned tutorial.

This tutorial will take you through the following steps:

We= b based Client

The following steps will help you in creating a Web based Client for a J= AX-WS Web Service which is already deployed on the server.

Create a Dynamic Web Project to consume the W= eb Service

    =09
  • From Eclipse main menu, select File->New->Other

  • =09
  • In the New dialog, select Web->Dynamic Web Project = and click Next

  • =09
  • Type jaxws-converterclient as the Project Name and cli= ck Next

  • =09
  • On the Project Facets page, the default selections are enough= .

  • =09
  • Make sure that the check box Generate Deployment Descriptor i= s selected and click Next

  • =09
  • On the Geronimo Deployment Page modify the Group Id to= org.apache.geronimo.samples.jaxws and the Artifact Id to = jaxws-converterclient.

  • =09
  • Click Finish

Creating necessary stubs to consume Web Service

We need to create Service Endpoint Interface based on the WSDL document = to use it in out JSP's. Luckily in Geronimo this can be automated using the= jaxws-tools utility provided by Geronimo.

<= /table>
    =09
  • Open a command prompt and point it to the bin directory of server in= stallation.

  • =09
  • We will use the jaxws-tools utility provided in the server's = bin directory to generate client stubs.

  • =09
  • Use the following command to generate the necessary stubs that are n= eeded to consume the web service.
Client Stubs Generation
<%SERVEER_INSTALLATION_BIN_DIR%>:\ jaxws-too=
ls.bat wsimport -s C:\WSDL http://localhost:80=
80/jaxws-conveter/converter?wsdl
    =09
  • Now all the stubs should have been created and placed in C:\WSDL<= /b> directory. We will be needing only the class ConverterPortType f= or accessing Web Service.

    =09
  • Copy the files Converter.java and ConverterPortType.java and place them in the appropriate folder according to their package decl= aration

Let us walk through the steps that we just completed

    =09
  • Converter.java - This file extends the javax.xml.ws.Servic= e and provides method to create a service and retrieve port which is th= e Service Endpoint Interface for the Web Service deployed.
    =09
  • ConverterPortType.java - This is the same file that is used t= o deploy the Web Service. This is the Service Endpoint Interface that is re= quired to access the Web Methods exposed by the Service.
    =09
  • jaxws-tools.bat wsimport - This is the utility in Geronimo wh= ich takes the help of jaxws-tools provided by Sun to create c= lient stubs

Developing the Web based Client

    =09
  • Right Click the jaxws-converterclient, and Select New->= JSP

  • =09
  • Name the jsp as index.jsp and click Finish

  • =09
  • Add the following code to the index.jsp
index.jsp
<%@ page language=3D=
"java" contentType=3D"text/html; charset=
=3DISO-8859-1"
=09pageEncoding=3D"ISO-8859-1"%>
<html>
<head>
<title>Converter</title>
<meta content=3D"text/html; CHARSET=3Diso-885=
9-1" http-equiv=3D"Content-Type"&g=
t;
</head>

<body>
<center>
<h3>This from invokes a Web Service.</h3>
<br>
Please type an amount and click submit to see the result.
<form action=3D"result.jsp">Amount(=
in Dollars): <input type=3D"text"
=09name=3D"amount"> <input type=3D<=
span class=3D"code-quote">"submit" value=3D"Submit"></form>
<br>
</center>
</body>
</html>
    =09
  • Right click again and add another jsp named result.jsp

  • =09
  • Add the following code to result.jsp
result.jsp
<%@ page language=3D=
"java" contentType=3D"text/html; charset=
=3DISO-8859-1"
=09pageEncoding=3D"ISO-8859-1"%>
<%@page import=3D"java.math.BigDecimal"%>
<%@ page import=3D"org.apache.geronimo.samples.jaxws.Converter"%>
<%@ page import=3D"org.apache.geronimo.samples.jaxws.ConverterPortType"%>
<html>
<head>
<title>Converter</title>
<meta content=3D"text/html; CHARSET=3Diso-885=
9-1" http-equiv=3D"Content-Type"&g=
t;
</head>
<body>
<%
=09String amount =3D request.getParamete=
r("amount");

=09if (amount !=3D null && amount.trim().length() > 0) {

=09=09out.println("<center>");

=09=09try {
=09=09=09BigDecimal dollars =3D new Big=
Decimal(amount);
                        Converter conv =3D new=
 Converter();
=09=09=09ConverterPortType port =3D conv.getConverterPort();
=09=09=09BigDecimal rupees =3D conv.dollarToRupees(dollars);
=09=09=09BigDecimal euros =3D conv.rupeesToEuro(rupees);

=09=09=09out.println(dollars + " Dollars equals =
to " + rupees
=09=09=09=09=09+ " Rupees");
=09=09=09out.println("<br>");
=09=09=09out.println(rupees + " Rupees equals to=
 " + euros
=09=09=09=09=09+ " Euros");

=09=09} catch (Exception e) {
=09=09=09out.println("Error: " + e.getMes=
sage());
=09=09}

=09=09out.println("<center>");
=09}
%>
</body>
</html>

This concludes the development section of our web based client.

Deploying and Testing the Web Client

Deploy
    =09
  • Right now our server status might be reporting that we need to repub= lish.

  • =09
  • Right click on the Apache Geronimo Server Runtime present in = the servers view and select Publish

  • =09
  • Wait for some time till the server status changes to Synchronized=

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

  • =09
  • In the popup, check the check box Always use this server when run= ning the project and then click Finish

  • =09
  • Now Eclipse will try to open the jsp in a web browser which shows yo= u a form to enter amount in Dollars.

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

3D""

Please take care that jaxws-tools.bat utility in server's bin directory = may not work in old versions of Apache Geronimo.
Try using the latest version of Geronimo i.e v2.1.1 .

3D""Converter

Note that we accessed the webservice in the jsp by creating a service= , and then retrieving the Converter port from the WSDL document.=

=20

POJO C= lient

Here we will have a look on how to create a plain old java client for co= nsuming the web service.

<= /a>Developing the Client

    =09
  • Create a Java Project =09
      =09=09
    • Select File->New->Project

    • =09=09
    • In the popup window select Java->Java Project and then = click Next

    • =09=09
    • Name the project as jaxws-converterpojoclient and click Finish in the New Java Project dialog.

    • =09
    =09
  • =09
  • Copy the files Converter.java and ConverterPortType.java into the appropriate package.

  • =09
  • Add another class with the name ConverterClient.java in the s= ame package.

  • =09
  • Add the following code to the ConverterClient.java
ConverterClient.java
=
package org.ap=
ache.geronimo.samples.jaxws;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;

public class ConverterClient {
=09public =
static void main(String args[]) {
=09=09try {
=09=09=09BufferedReader dataIn =3D new =
BufferedReader(new InputStreamReader(
=09=09=09=09=09System.in));
=09=09=09System.out.println("enter a amount to convert:");
=09=09=09String number =3D new String();
=09=09=09number =3D dataIn.readLine();
=09=09=09BigDecimal amount =3D new BigD=
ecimal(number);
=09=09=09Converter conv =3D new Convert=
er();
=09=09=09ConverterPortType port =3D conv.getConverterPort();
=09=09=09BigDecimal result =3D port.dollarToRupees(amount);
=09=09=09System.out.println(amount + " dollars equals to " + result
=09=09=09=09=09+ " rupees");
=09=09=09BigDecimal result1 =3D port.rupeesToEuro(result);
=09=09=09System.out.println(result + " rupees equals to " + result1
=09=09=09=09=09+ " euros");
=09=09} catch (Exception e) {
=09=09=09e.printStackTrace();
=09=09}
=09}

}

This completes the development section of our POJO client

Adding necessary jar files to the class path

3D""Errors in Java Project

Now our Java project might show many errors and even if we resolve the e= rrors the application may not run properly. This is due to the missing jar = files required to access and understand SOAP messages sent by web service.<= /p>

    =09
  • Right click the jaxws-converterpojoclient project and then se= lect Properties

  • =09
  • Select Java Build Path and then go to Libraries tab

  • =09
  • Click on Add External JARS and then add the axis2-adb-1.3.= jar from the location <GERONIMO_INSTALL_DIR>/repository/org/ap= ache/axis2/axis2-adb/1.3/ and then click Open
    3D""Version Numbers

    The version numbers may differ slightly. It wont matter much.

  • =09
  • Repeat the above step for the following external JARS
External JARS Required

axis2-adb-1.3.jar - <GERONIMO_INSTALL_DIR>/repository/org/a= pache/axis2/axis2-adb/1.3/axis2-adb-1.3.jar
axis2-java2wsdl-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\or= g\apache\axis2\axis2-java2wsdl\1.3\axis2-java2wsdl-1.3.jar
axis2-jaxws-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org\ap= ache\axis2\axis2-jaxws\1.3\axis2-jaxws-1.3.jar
axis2-kernel-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org\a= pache\axis2\axis2-kernel\1.3\axis2-kernel-1.3.jar
axis2-metadata-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org= \apache\axis2\axis2-metadata\1.3\axis2-metadata-1.3.jar
axis2-saaj-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\org\apa= che\axis2\axis2-saaj\1.3\axis2-saaj-1.3.jar
axiom-api-1.2.5.jar - <GERONIMO_INSTALL_DIR>\repository\org\ap= ache\ws\commons\axiom\axiom-api\1.2.5\axiom-api-1.2.5.jar
axiom-dom-1.2.5.jar - <GERONIMO_INSTALL_DIR>\repository\org\ap= ache\ws\commons\axiom\axiom-dom\1.2.5\axiom-dom-1.2.5.jar
axiom-impl-1.2.5.jar - <GERONIMO_INSTALL_DIR>\repository\org\a= pache\ws\commons\axiom\axiom-impl\1.2.5\axiom-impl-1.2.5.jar
XmlSchema-1.3.1.jar - <GERONIMO_INSTALL_DIR>\repository\org\ap= ache\ws\commons\schema\XmlSchema\1.3.1\XmlSchema-1.3.1.jar
neethi-2.0.jar - <GERONIMO_INSTALL_DIR>\repository\org\apache\= neethi\neethi\2.0\neethi-2.0.jar
wsdl4j-1.6.1.jar - <GERONIMO_INSTALL_DIR>\repository\wsdl4j\ws= dl4j\1.6.1\wsdl4j-1.6.1.jar
xml-resolver-1.1.jar - <GERONIMO_INSTALL_DIR>\repository\xml-r= esolver\xml-resolver\1.1\xml-resolver-1.1.jar
xml-beans-2.3.0.jar - <GERONIMO_INSTALL_DIR>\repository\org\ap= ache\xmlbeans\xmlbeans\2.3.0\xmlbeans-2.3.0.jar
commons-codec-1.3.jar - <GERONIMO_INSTALL_DIR>\repository\comm= ons-codec\commons-codec\1.3\commons-codec-1.3.jar
commons-httpclient-3.0.1.jar - <GERONIMO_INSTALL_DIR>\reposito= ry\commons-httpclient\commons-httpclient\3.0.1\commons-httpclient-3.0.1.jar=
wstx-asl-3.2.1.jar - <GERONIMO_INSTALL_DIR>\repository\woodsto= x\wstx-asl\3.2.1\wstx-asl-3.2.1.jar

Also add Server Runtime Library to class path which reduces the effort f= or adding some more jars.

This completes the adding external JARs to the project

3D""JRE 6

If you are running Java 6 you don't need to worry about the external JAR= s as they are all inbuilt into JRE 6. But you should copy all the files gen= erated by wsimport command into appropriate packages as Runtime Modeller in= Java 6 is slightly different.

=20

Testing
    =09
  1. Now Right click the ConverterClient.class and select Run A= s->Run as Java Application

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

This completes the development and deployment of clients for consuming a= Web Service. Even though this tutorial demonstrated for one particular Web= Service, the method can be extended for any deployed web service.