Anda di halaman 1dari 4

Creating a Web Service application using a WSDL

document

The following setup is required on your machine before we begin.


1. JDK 1.5 or higher version
2. JWSDP 1.6
3. Eclipse JEE version (Galileo is used in this demonstration)
4. Tomcat 5.5 or higher

The Sun JWSDP 1.6 by default is installed in C:\Sun\jwsdp-1.6, which I will refer to
as JWSDP_HOME. Let us also create a work folder C:\EXAMPLES\

Follow these steps:


1. Create a WSDL document (ex: CustomerService.wsdl) or copy one from a
source.
2. We use the wscompile command provided by the Sun JWSDP, which needs
the wsdl document from a configuration file. So, let us create config.xml
(name can be anything) having the following code:

<?xml version="1.0" ?>

<configuration
xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<wsdl location="customer-service.wsdl"
packageName="com.vinod.service" />
</configuration>

3. Open a command prompt and change the directory to C:\EXAMPLES


4. Run the setenv.bat from the %JWSDP_HOME%\jwsdp-shared\bin directory
to set the JWSDP environment (such as path/classpath required)
5. Add the %JWSDP_HOME%\jaxrpc\bin to the path

PATH=%PATH%;C:\Sun\jwsdp-1.6\jaxrpc\bin

6. Now run the wscompile command as shown below to generate the


serverside artifacts required for our web service

wscompile -keep -gen:server config.xml

© 2009 - www.javatrainer.org 1/4


7. The following files are generated in this demonstration under the folder
com\vinod\service (the package directory specified in the config.xml)

• Customer.class
• Customer_LiteralSerializer.class
• CustomerServicePortType.class
• CustomerService_SerializerRegistry.class
• CustomerServicePortType_Tie.class
• CustomerServicePortType.java
• CustomerService_SerializerRegistry.java
• Customer.java
• Customer_LiteralSerializer.java
• CustomerServicePortType_Tie.java

The class com.vinod.service.Customer is the result of JAXB operation on the


schema.
The interface com.vinod.service.CustomerServicePortType is our SEI (Service
Endpoint Interface, to be implemented by us which is the actual business logic that
serves the client).
The class com.vinod.service.CustomerServicePortType_Tie is the tie class that
translates the incoming SOAP request to Java method calls, and returns a SOAP
response converting the return value (or exception) from the method calls.

Now that the serverside artifacts are ready, it is time to create the web application to
deploy our web service
1. Open Eclipse and create a dynamic web project named
CustomerInfoWSApp (or something that suites your application)
2. Copy the following jar files into your application’s WEB-INF\lib folder

• %JWSDP_HOME%\fastinfoset\lib\FastInfoset.jar
• %JWSDP_HOME%\jaxrpc\lib\jaxrpc-api.jar
• %JWSDP_HOME%\jaxrpc\lib\jaxrpc-impl.jar
• %JWSDP_HOME%\jaxrpc\lib\jaxrpc-spi.jar
• %JWSDP_HOME%\saaj\lib\saaj-api.jar
• %JWSDP_HOME%\saaj\lib\saaj-impl.jar
• %JWSDP_HOME%\sjsxp\lib\jsr173_api.jar
• %JWSDP_HOME%\jwsdp-shared\lib\activation.jar
• %JWSDP_HOME%\jwsdp-shared\lib\mail.jar

3. We must redirect all the client requests for the web service endpoint to a
servlet (com.sun.xml.rpc.server.http.JAXRPCServlet) that is supplied with
JWSDP. This is done by making the following additions to the web.xml of our
application

© 2009 - www.javatrainer.org 2/4


<servlet>
<servlet-name>JAXRPCServlet</servlet-name>
<servlet-class>
com.sun.xml.rpc.server.http.JAXRPCServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAXRPCServlet</servlet-name>
<url-pattern>/CustomerService</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
com.sun.xml.rpc.server.http.JAXRPCContextListener
</listener-class>
</listener>

4. The servlet com.sun.xml.rpc.server.http.JAXRPCServlet during startup looks


for a configuration file jaxrpc-ri-runtime.xml (Sun specific). Create the
same in the WEB-INF folder of your application. The content of the file is
listed below:

<?xml version="1.0"?>

<endpoints
xmlns='http://java.sun.com/xml/ns/jax-rpc/ri/runtime'
version="1.0">

<endpoint
name="customerservice"
interface="com.vinod.service.CustomerServicePortType"
implementation
="com.vinod.service.CustomerServicePortType_Impl"
tie="com.vinod.service.CustomerServicePortType_Tie"
wsdl="/WEB-INF/CustomerService.wsdl"
service="CustomerService"
port="CustomerServicePort"
urlpattern="/CustomerService"/>
</endpoints>

Whenever a client request is received by the JAXRPCServlet, it will refer to


the information given in this file and looks for the urlpattern and finds the
corresponding tie class. The tie class then finds the name of the method in
the SOAP request and calls the same on the implementation class. Note that I
have highlighted the name of the implementation class. As of now, this class
does not exisit. We have to create this class implementing the SEI
(com.vinod.service.CustomerServicePortType)

5. Create a package com.vinod.service (this must match the one used in the
config.xml) and copy all the Java source files to this package

© 2009 - www.javatrainer.org 3/4


6. Create the Service Implementation Class CustomerServicePortType_Impl, in
the same package. Make sure that this class implements the SEI.
7. Copy the WSDL document to the WEB-INF folder
8. Deploy the web application.

If everything goes well, you must be able to see the service and the WSDL from the
following locations:

http://localhost:8080/CustomerInfoWSApp/CustomerService
http://localhost:8080/CustomerInfoWSApp/CustomerService?WSDL

© 2009 - www.javatrainer.org 4/4

Anda mungkin juga menyukai