<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java, Programming, Struts2, jQuery, Linux and more &#187; axis2</title>
	<atom:link href="http://www.jgeppert.com/category/java/axis2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jgeppert.com</link>
	<description>Johannes Geppert</description>
	<lastBuildDate>Thu, 09 Feb 2012 16:38:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Ein Webservice mit Axis2 und Spring</title>
		<link>http://www.jgeppert.com/2009/02/webservice-mit-axis2-und-spring/</link>
		<comments>http://www.jgeppert.com/2009/02/webservice-mit-axis2-und-spring/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 18:16:42 +0000</pubDate>
		<dc:creator>jogep</dc:creator>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[axis2]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[webservices]]></category>
		<category><![CDATA[webservice]]></category>

		<guid isPermaLink="false">http://www.jgeppert.com/?p=165</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.jgeppert.com/2009/02/webservice-mit-axis2-und-spring/' addthis:title='Ein Webservice mit Axis2 und Spring '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Mit Axis2 ist das Erstellen von Webservices für Java Programmierer noch einfacher geworden. Mit folgendem kleinen Beispiel will ich zeigen, wie man einen Webservice programmieren kann, der von Spring ein Bean injiziert bekommt. Als Erstes erstellen wir unseren Webservice als einfache Java Klasse: package com.jgeppert.services; &#160; import com.jgeppert.dao.MyDAO; &#160; private MyDAO myDAO; &#160; public class [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2009/02/webservice-mit-axis2-und-spring/' addthis:title='Ein Webservice mit Axis2 und Spring ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.jgeppert.com/2009/02/webservice-mit-axis2-und-spring/' addthis:title='Ein Webservice mit Axis2 und Spring '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div><p>Mit <a href="http://ws.apache.org/axis2/">Axis2</a> ist das Erstellen von Webservices für Java Programmierer noch einfacher geworden. Mit folgendem kleinen Beispiel will ich zeigen, wie man einen Webservice programmieren kann, der von <a href="http://www.springsource.org">Spring</a> ein Bean injiziert bekommt.
</p>
<p>
Als Erstes erstellen wir unseren Webservice als einfache Java Klasse:
</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jgeppert.services</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.jgeppert.dao.MyDAO</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> MyDAO myDAO<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyWebService
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">/* Die Funktion add() ist unsere Webservice Operation die
	 * dann mit einem Client angesprochen werden kann.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> add<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> text<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			myDAO.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;insert successfully&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">RuntimeException</span> e<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">return</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Mit dieser Methode injiziert uns Spring unser DAO Objekt</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> setMyDAO<span style="color: #009900;">&#40;</span>MyDAO myDAO<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">myDAO</span> <span style="color: #339933;">=</span> myDAO<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



<p>
Als Zweites brauchen wir für Aris2 eine service.xml mit folgendem Inhalt:
</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My First WebService<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ServiceClass&quot;</span> <span style="color: #000066;">locked</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		com.jgeppert.services.MyWebService
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/parameter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ServiceObjectSupplier&quot;</span> <span style="color: #000066;">locked</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/parameter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;parameter</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;SpringBeanName&quot;</span> <span style="color: #000066;">locked</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>myWebService<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/parameter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;add&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;messageReceiver</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.axis2.rpc.receivers.RPCMessageReceiver&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/operation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/service<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>



<p>
Für Spring sind die zwei Parameter <strong>ServiceObjectSupplier</strong> und <strong>SpringBeanName</strong> entscheidend, um uns das benötigte DAO zu übergeben.

Mit Hilfe eines <a href="http://ws.apache.org/axis2/1_4_1/pojoguide.html#buildpojows">Ant Scriptes</a> oder manuell kann man sich den Webservice in das für Axis2 benötigte AAR Archiv packen. Das Archiv ist im Grunde eine normale JAR Datei mit der service.xml im META-INF Verzeichnis.
</p>
<p>
Die Struktur des AAR-Archives sollte so aus sehen:
</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">-</span> MyWebService
   <span style="color: #339933;">-</span> META<span style="color: #339933;">-</span>INF
     <span style="color: #339933;">-</span> services.<span style="color: #006633;">xml</span>
   <span style="color: #339933;">-</span> com
     <span style="color: #339933;">-</span> jgeppert
       <span style="color: #339933;">-</span> services
         <span style="color: #339933;">-</span> MyWebService.<span style="color: #000000; font-weight: bold;">class</span></pre></div></div>



<p>
Damit Spring unser DAO in dem Service injizieren kann, brauchen wir noch folgenden Eintrag in unserer applicationContext.xml.</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;applicationContext&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myWebService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.jgeppert.services.MyWebService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;myDAO&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;MyDAO&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>



<p>
Nach dem deployen des AAR-Archives im services Folder der Axis2 Web Application steht der Service nun zur Verfügung.
</p><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2009/02/webservice-mit-axis2-und-spring/' addthis:title='Ein Webservice mit Axis2 und Spring ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.jgeppert.com/2009/02/webservice-mit-axis2-und-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

