<?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; restful</title>
	<atom:link href="http://www.jgeppert.com/tag/restful/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>
	</channel>
</rss>

