<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>&amp;lt;blog&amp;gt;Matt Osentoski&amp;lt;/blog&amp;gt;: Webservices using Axis</title>
    <link>http://matt.osentoski.com/articles/2007/02/23/webservices-using-axis</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Webservices using Axis</title>
      <description>This article is a howto that describes setting up a web service using the 'Bottom Up' approach.
&lt;br /&gt;&lt;br /&gt;
&lt;h3&gt;Intro&lt;/h3&gt;
The first step to building web services is to figure out which method
to start with.  (Top down or Bottom up)
&lt;br /&gt;
&lt;h3&gt;Bottom Up Development&lt;/h3&gt;
Creating a web service from a Java Bean.
This method creates a WSDL file automatically from Java code.  To get this
type of system to work, you will have to create a WSDD file which describes
which java objects should be exposed as webservices. This approach is easier 
in the sense that you don't have to create a WSDL file, however the 
downside is that there is a greater risk that the WSDL file generated will 
not work with other systems.  I would recommend trying Bottom Up development first.  You can switch to Top Down development if you run into
severe interoperability problems.
&lt;br /&gt;
&lt;h3&gt;Top Down Development&lt;/h3&gt;
This approach starts with the WSDL file.
Java files are then generated from WSDL using a utility (WSDL2Java).  This method has the highest
chance of interoperability, but requires that you create a WSDL file from 
scratch, which can be very difficult and error prone process without the help of 
tools.
&lt;br /&gt; 
&lt;h3&gt;Bottom Up Development (Using JWS files)&lt;/h3&gt;
Create a web service from a Java Bean that is renamed with the .jws extension
This method creates a web service from a Java bean without the need to 
create a WSDD or WSDL file.  It is the easiest approach to use, but also
the most limiting in terms of functionality and interoperability.  &lt;strong&gt;This
method should NOT be used!!&lt;/strong&gt;


&lt;br /&gt;
&lt;h3&gt;Development (Bottom Up) - Server &lt;/h3&gt;
Assuming you've chosen to develop using the Bottom Up approach, begin by 
creating your Java Objects.  I generally create three layers:&lt;br /&gt;
1) &lt;strong&gt;Business Object Layer.&lt;/strong&gt;  This layer consist of the Beans that make up my model&lt;br /&gt;
2) &lt;strong&gt;DAO layer for database persistance.&lt;/strong&gt;  (Using Hibernate or JDBC)&lt;br /&gt;
3) &lt;strong&gt;Service Layer.&lt;/strong&gt;  This is the loosely grained layer used for making the 
web service calls (Ex: getCustomerByName(String name)).  This is also the 
layer that will generate the WSDL file(s).&lt;br /&gt;
&lt;br /&gt;
(NOTE: Save yourself a lot of pain and write some tests.  There's
nothing worse than trying to resolve an obscure Hibernate error while end-to-end
testing your services)
&lt;br /&gt;&lt;br /&gt;
Now that your Java code has been written (and hopefully tested ;) ) you can
begin to write your WSDD file.  The purpose of the WSDD file is to describe
which java class will be exposed using web services.  See &lt;a href="/files/server-config.wsdd"&gt;this WSDD&lt;/a&gt; file as an
example.  The most important part is the comment section, I included, called 'Put your service definitions here'.  Follow the example to add your own classes.  If you're deploying your web
services as a web application, the WSDD file should be called 'server-config.wsdd'
and placed under your /WEB-INF directory.  The Axis servlet will look for this
file on startup. (This also saves you the annoyance of using the Axis admin tools for 
service startup.)
&lt;br /&gt;&lt;br /&gt;
Now you will need to add the following Axis 1.2 library dependencies under /WEB-INF/lib:&lt;br /&gt;
axis.jar&lt;br /&gt;
commons-discovery.jar&lt;br /&gt;
commons-logging.jar&lt;br /&gt;
jaxrpc.jar&lt;br /&gt;
log4j-1.2.8.jar  (your version may vary)&lt;br /&gt;
saaj.jar&lt;br /&gt;
wsdl-4j-1.5.1.jar  (again.. your version may vary)&lt;br /&gt;
&lt;br /&gt;
All of these libraries are included with Axis. &lt;br /&gt; 
&lt;br /&gt;
Next, you will have to modify your web.xml file to startup the Axis servlet
and services.  See &lt;a href="/files/web.xml"&gt;this web.xml&lt;/a&gt; file as an example.
&lt;br /&gt;&lt;br /&gt;
At this point you can compile all of your Java code into /WEB-INF/lib (as a jar) or
/WEB-INF/classes and deploy the War file.  Your web services should now be running.&lt;br /&gt;
To check, try accessing your services from a web browser:&lt;br /&gt;
http://localhost:8080/yourAppName/services/SomeService?wsdl
&lt;br /&gt;&lt;br /&gt;
(Note: 'YourAppName' is the name of your application context.  'SomeService'
will be the name of your service as defined in the WSDD file.)
&lt;br /&gt;&lt;br /&gt;
This will display the WSDL file that is dynamically created.  To call a method
use: &lt;br /&gt;
http://localhost:8080/yourAppName/services/SomeService?method=aMethodThatDoesCoolStuff
&lt;br /&gt;&lt;br /&gt;
(NOTE: 'aMethodThatDoesCoolStuff' would be the name of a method in your service layer.
You can also set method params by adding paramName=paramValue pairs at the end
of the URL)
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;Development - Client &lt;/h3&gt;
The client code is automatically created using a utility called WSDL2Java.
See &lt;a href="/files/wsdl_run.bat"&gt;this wsdl_run.bat&lt;/a&gt; script for an example that creates client code.  A simple example
for creating client stubs is also below:&lt;br /&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;java -cp %CLASS_PATH% org.apache.axis.wsdl.WSDL2Java -o .\src -d Session -p test.ws http://localhost:8080/myapp/services/MyService?wsdl&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
This command will create all the model objects and SOAP bindings.  There will
be a class called MyServiceSoapBindingStub that you will instantiate using another
class called MyServiceImplServiceLocator to make web service calls.  Compile all the generated code
then call the web service using the example below.
&lt;br /&gt;&lt;br /&gt;
Example:&lt;br /&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&lt;notextile&gt;MyServiceImplServiceLocator locator = new MyServiceImplServiceLocator();
MyServiceSoapBindingStub soapCall = (MyServiceSoapBindingStub) locator.getMyService();
String coolText = soapCall.aMethodThatDoesSomethingCool();
System.out.println(&amp;quot;Output from WS: &amp;quot; + coolText + &amp;quot;\n\nYes! It works!!&amp;quot;);&lt;/notextile&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Fri, 23 Feb 2007 09:56:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:fb219bdd-cdf8-4f29-b35a-41d8ce554538</guid>
      <author>Matt Osentoski</author>
      <link>http://matt.osentoski.com/articles/2007/02/23/webservices-using-axis</link>
      <category>Web Services</category>
      <category>Axis</category>
      <category>wsdl</category>
      <category>Java</category>
    </item>
  </channel>
</rss>
