<?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; jersey</title>
	<atom:link href="http://www.jgeppert.com/tag/jersey/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>Java Client für REST WebService (JAX-RS)</title>
		<link>http://www.jgeppert.com/2009/05/java-client-fuer-restful-web-services/</link>
		<comments>http://www.jgeppert.com/2009/05/java-client-fuer-restful-web-services/#comments</comments>
		<pubDate>Mon, 25 May 2009 13:19:18 +0000</pubDate>
		<dc:creator>jogep</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[webservices]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[jax-rc]]></category>
		<category><![CDATA[jersey]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[rest client]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[restservice]]></category>
		<category><![CDATA[web service]]></category>
		<category><![CDATA[webservice]]></category>
		<category><![CDATA[webservice client]]></category>

		<guid isPermaLink="false">http://www.jgeppert.com/?p=233</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.jgeppert.com/2009/05/java-client-fuer-restful-web-services/' addthis:title='Java Client für REST WebService (JAX-RS) '  ><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>Nach dem Erstellen eines REST WebServices mit JAX-RS habe ich schon ein Beispiel für einen PHP Client eines WebServices gegeben. Natürlich lässt sich der REST WebService auch mit Java exzellent verarbeiten. Wie das geht werde ich in einem kleinen Beispiel demonstrieren. Der Client ruft den REST Web Service aus dem Tutorial für REST WebServices mit [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2009/05/java-client-fuer-restful-web-services/' addthis:title='Java Client für REST WebService (JAX-RS) ' ><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/05/java-client-fuer-restful-web-services/' addthis:title='Java Client für REST WebService (JAX-RS) '  ><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>Nach dem Erstellen eines <a href="http://www.jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey/">REST WebServices mit JAX-RS</a> habe ich schon ein Beispiel für einen <a href="http://www.jgeppert.com/2009/05/php-client-rest-webservice/">PHP Client eines WebServices</a> gegeben. Natürlich lässt sich der REST WebService auch mit Java exzellent verarbeiten. Wie das geht werde ich in einem kleinen Beispiel demonstrieren. Der Client ruft den REST Web Service aus dem <a href="http://www.jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey/">Tutorial für REST WebServices mit Jersey</a> auf und benötigt auch die meisten JAR-Files aus diesem Beispiel. Zusätzlich wird noch das <a title="Jersay Client Jar" href="http://download.java.net/maven/2/com/sun/jersey/jersey-client/1.0.2/jersey-client-1.0.2.jar">jersey-client-1.0.2.jar</a> benötigt.

Jersey stellt uns für den einfachen Zugriff die Klassen <em>Client</em> und <em>WebResource</em> bereit.


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">		Client client <span style="color: #339933;">=</span> Client.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		WebResource wr <span style="color: #339933;">=</span> client.<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://localhost:8080/wstest/services/calculator/calc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>




Mit Hilfe von <em>WebResource</em> können wir mit den HTTP Methoden <em>post()</em>, <em>delete()</em>, <em>put()</em> und natürlich <em>get()</em> unseren WebService aufrufen. Und mit der <em>MultivaluedMap</em> ist es uns möglich, dem WebService unsere Abfrageparameter mitzugeben.


<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;">de.wstest.client</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.core.MediaType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.core.MultivaluedMap</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.jersey.api.client.Client</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.jersey.api.client.GenericType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.jersey.api.client.WebResource</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.jersey.core.util.MultivaluedMapImpl</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">de.wstest.MathResults</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">de.wstest.ServiceResult</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CalculatorClient <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		Client client <span style="color: #339933;">=</span> Client.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//URL des WebService</span>
		WebResource wr <span style="color: #339933;">=</span> client.<span style="color: #006633;">resource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://localhost:8080/wstest/services/calculator/calc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Parameter für WebService</span>
		MultivaluedMap<span style="color: #339933;">&lt;</span> <span style="color: #003399;">String</span>, <span style="color: #003399;">String</span> <span style="color: #339933;">&gt;</span> params <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MultivaluedMapImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		params.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span>, <span style="color: #0000ff;">&quot;234&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		params.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;b&quot;</span>, <span style="color: #0000ff;">&quot;567&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		ServiceResult sr <span style="color: #339933;">=</span> wr.<span style="color: #006633;">queryParams</span><span style="color: #009900;">&#40;</span>params<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">accept</span><span style="color: #009900;">&#40;</span>MediaType.<span style="color: #006633;">APPLICATION_XML_TYPE</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GenericType<span style="color: #339933;">&lt;</span> ServiceResult <span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Ergebnis ausgeben</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>sr.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span> MathResults <span style="color: #339933;">&gt;</span> mathResults <span style="color: #339933;">=</span> sr.<span style="color: #006633;">getErgebnisse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>MathResults mr <span style="color: #339933;">:</span> mathResults<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>mr.<span style="color: #006633;">getFormel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot; = &quot;</span><span style="color: #339933;">+</span>mr.<span style="color: #006633;">getErgebniss</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




Die Ausgabe des WebServices ist wenig überraschend:
<code>
Results for Value a = 234, Value b = 567
a + b = 801
a - b = -333
a * b = 132678
</code>

<strong>FAZIT:</strong>
Wie man sieht, ist mit Hilfe der Jersey Klassen eine einfache Abfrage von REST WebServices möglich.

Ausführlichere Beispiele findet man auf den <a href="http://blogs.sun.com/enterprisetechtips/entry/consuming_restful_web_services_with">Enterprise Tech Tips</a> Seiten von SUN, in welchem die Twitter Webservices benutzt werden. Dort gibt es dann auch Beispiele für das Benutzen der PUT, POST und DELETE Methoden.<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2009/05/java-client-fuer-restful-web-services/' addthis:title='Java Client für REST WebService (JAX-RS) ' ><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/05/java-client-fuer-restful-web-services/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RESTful Web Services (JAX-RS) mit Jersey</title>
		<link>http://www.jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey/</link>
		<comments>http://www.jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 10:59:32 +0000</pubDate>
		<dc:creator>jogep</dc:creator>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[webservices]]></category>
		<category><![CDATA[jax-rc]]></category>
		<category><![CDATA[jersey]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[web service]]></category>
		<category><![CDATA[webservice]]></category>

		<guid isPermaLink="false">http://www.jgeppert.com/?p=189</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey/' addthis:title='RESTful Web Services (JAX-RS) mit Jersey '  ><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>Durch das Jersey Projekt existieren Bibliotheken die es Java Entwicklern besonders einfach machen REST Web Services zu entwickeln. In diesem Tutorial werde ich zeigen wie einfach es ist eine Web Service zu schreiben der uns einfache Mathematische Berechnung zurückliefert. 1.) Wir erstellen in unserer Entwicklungsumgebung, in meinem Beispiel Eclipse, ein Web Projekt.  In das WEB-INF/lib [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey/' addthis:title='RESTful Web Services (JAX-RS) mit Jersey ' ><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/03/restful-web-services-jax-rs-mit-jersey/' addthis:title='RESTful Web Services (JAX-RS) mit Jersey '  ><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>Durch das <a href="https://jersey.dev.java.net/">Jersey</a> Projekt existieren Bibliotheken die es Java Entwicklern besonders einfach machen REST Web Services zu entwickeln. In diesem Tutorial werde ich zeigen wie einfach es ist eine Web Service zu schreiben der uns einfache Mathematische Berechnung zurückliefert.

1.)
Wir erstellen in unserer Entwicklungsumgebung, in meinem Beispiel Eclipse, ein Web Projekt.  In das <strong>WEB-INF/lib</strong> Verzeichnis packen wir alle für Jersey benötigten JARs die es bei <a href="https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/jersey/dependencies.html">Jersey/Downloads</a> runterzuladen gibt.  Für dieses Tutorial benötigen wir folgende Bibliotheken:<a href="http://download.java.net/maven/2/com/sun/jersey/jersey-server/1.0.2/jersey-server-1.0.2.jar"> jersey-server.jar</a>, <a href="http://download.java.net/maven/2/com/sun/jersey/jersey-core/1.0.2/jersey-core-1.0.2.jar">jersey-core.jar</a>, <a href="http://download.java.net/maven/2/javax/ws/rs/jsr311-api/1.0/jsr311-api-1.0.jar">jsr311-api.jar</a>, <a href="http://repo1.maven.org/maven2/asm/asm/3.1/asm-3.1.jar">asm.jar, </a><a href="http://download.java.net/maven/1/com.sun.xml.bind/jars/jaxb-impl-2.1.jar">jaxb-impl.jar</a>, <a href="http://download.java.net/maven/1/javax.xml.bind/jars/jaxb-api-2.1.jar">jaxb-api.jar</a>, <a href="http://download.java.net/maven/1/javax.activation/jars/activation-1.1.jar">activation.jar</a>, <a href="http://download.java.net/maven/1/javax.xml.stream/jars/stax-api-1.0-2.jar">stax-api.jar</a>

2.)
In unserer <strong>WEB-INF/web.xml</strong> fügen wir folgendes hinzu:


<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;servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>JAX-RS REST Servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/display-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>REST-Servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  		com.sun.jersey.spi.container.servlet.ServletContainer
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/load-on-startup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>REST-Servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/services/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/servlet-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>




3.)
Wir erstellen zwei Klassen die wir für unser Ergebniss benötigen.

Die erste Klasse <strong>MathResults</strong> kapselt unsere Mathematischen Ergebnisse.


<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;">de.wstest</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MathResults <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> formel<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> ergebniss<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getFormel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> formel<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFormel<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> formel<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;">formel</span> <span style="color: #339933;">=</span> formel<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getErgebniss<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> ergebniss<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setErgebniss<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> ergebniss<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;">ergebniss</span> <span style="color: #339933;">=</span> ergebniss<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



Und die zweite Klasse <strong>ServiceResult</strong> ist das Result unseres WebService


<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;">de.wstest</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.bind.annotation.XmlRootElement</span><span style="color: #339933;">;</span>
&nbsp;
@XmlRootElement
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ServiceResult <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Integer</span> returncode<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>MathResults<span style="color: #339933;">&gt;</span> ergebnisse<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> getReturncode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> returncode<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setReturncode<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span> returncode<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;">returncode</span> <span style="color: #339933;">=</span> returncode<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> message<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<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;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> List<span style="color: #339933;">&lt;</span>MathResults<span style="color: #339933;">&gt;</span> getErgebnisse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> ergebnisse<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setErgebnisse<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>MathResults<span style="color: #339933;">&gt;</span> ergebnisse<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;">ergebnisse</span> <span style="color: #339933;">=</span> ergebnisse<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




4.)
Jetzt erstellen wir eine weitere Klasse <strong>CalculatorService</strong>, diese ist unser eigentlicher Webservice und wird durch Annotions als dieser gekennzeichnet. Ein gute Übersicht über zulässige Annotions findet Ihr im <a href="http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features">Wiki von Jersey</a>.


<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;">de.wstest</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.LinkedList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.GET</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.Path</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.Produces</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ws.rs.QueryParam</span><span style="color: #339933;">;</span>
&nbsp;
@Produces<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/xml&quot;</span><span style="color: #009900;">&#41;</span>
@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;calculator&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CalculatorService <span style="color: #009900;">&#123;</span>
&nbsp;
	@GET
	@Path<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;calc&quot;</span><span style="color: #009900;">&#41;</span>
	@Produces<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/xml&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> ServiceResult add<span style="color: #009900;">&#40;</span>@QueryParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Integer</span> a, @QueryParam<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Integer</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		ServiceResult r <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ServiceResult<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span>MathResults<span style="color: #339933;">&gt;</span> ergs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedList<span style="color: #339933;">&lt;</span>MathResults<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> b <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			r.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No Values!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			r.<span style="color: #006633;">setReturncode</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			r.<span style="color: #006633;">setErgebnisse</span><span style="color: #009900;">&#40;</span>ergs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		r.<span style="color: #006633;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Results for Value a = &quot;</span><span style="color: #339933;">+</span>a<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, Value b = &quot;</span><span style="color: #339933;">+</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		r.<span style="color: #006633;">setReturncode</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		MathResults e1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MathResults<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		e1.<span style="color: #006633;">setFormel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a + b&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		e1.<span style="color: #006633;">setErgebniss</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">+</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		MathResults e2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MathResults<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		e2.<span style="color: #006633;">setFormel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a - b&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		e2.<span style="color: #006633;">setErgebniss</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">-</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		MathResults e3 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MathResults<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		e3.<span style="color: #006633;">setFormel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a * b&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		e3.<span style="color: #006633;">setErgebniss</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">*</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		ergs.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>e1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ergs.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>e2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		ergs.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>e3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		r.<span style="color: #006633;">setErgebnisse</span><span style="color: #009900;">&#40;</span>ergs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>




Fertig ist unsere RESTFull Webservice. Nach dem deployen in einem ServletContainer wie Tomcat erhalten wir nach einem Aufruf des Webservices im Browser

<em>http://localhost:8080/wstest/services/calculator/calc?a=234&amp;b=456</em>

folgendes Ergebniss:


<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;serviceResult<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ergebnisse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ergebniss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>690<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ergebniss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;formel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>a + b<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/formel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ergebnisse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ergebnisse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ergebniss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-222<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ergebniss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;formel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>a - b<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/formel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ergebnisse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ergebnisse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ergebniss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>106704<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ergebniss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;formel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>a * b<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/formel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ergebnisse<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Value a = 234, Value b = 456<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/message<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;returncode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/returncode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/serviceResult<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>




Durch einen Aufruf der URL

<em>http://localhost:8080/wstest/services/application.wadl</em>

erhalten wir eine Übersicht über unsere Services und deren Parameter.


<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;application</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://research.sun.com/wadl/2006/10&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;doc</span> <span style="color: #000066;">xmlns:jersey</span>=<span style="color: #ff0000;">&quot;http://jersey.dev.java.net/&quot;</span> <span style="color: #000066;">jersey:generatedBy</span>=<span style="color: #ff0000;">&quot;Jersey: 1.0 10/13/2008 12:27 PM&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resources</span> <span style="color: #000066;">base</span>=<span style="color: #ff0000;">&quot;http://localhost:8080/wstest/services/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;calculator&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;calc&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;GET&quot;</span> <span style="color: #000066;">id</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;request<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">xmlns:xs</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xs:int&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;query&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">xmlns:xs</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xs:int&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;query&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;b&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;/request<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;response<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;representation</span> <span style="color: #000066;">mediaType</span>=<span style="color: #ff0000;">&quot;application/xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/response<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/method<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>




Fertig ist in wenigen Schritten unser REST Web Service der uns eine Liste von Berechnungen für einen Eingabe zurückliefert.
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.jgeppert.com/2009/03/restful-web-services-jax-rs-mit-jersey/' addthis:title='RESTful Web Services (JAX-RS) mit Jersey ' ><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/03/restful-web-services-jax-rs-mit-jersey/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

