blob: d4fa5b126f816012f855d2637c0550730753877a [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Apache Wink : 6.1 Getting Started with Apache Wink Client</title>
<link rel="stylesheet" href="styles/site.css" type="text/css" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
<tr>
<td valign="top" class="pagebody">
<div class="pageheader">
<span class="pagetitle">
Apache Wink : 6.1 Getting Started with Apache Wink Client
</span>
</div>
<div class="pagesubheading">
This page last changed on Oct 13, 2009 by <font color="#0050B2">michael</font>.
</div>
<h2><a name="6.1GettingStartedwithApacheWinkClient-GettingStartedwiththeApacheWinkClient"></a>Getting Started with the Apache Wink Client</h2>
<p>The following section details the getting started examples that demonstrate how to write a simple client that consume RESTful Web Services with the Apache Wink Client.</p>
<h3><a name="6.1GettingStartedwithApacheWinkClient-GETRequest"></a>GET Request</h3>
<p>The following example demonstrates how to issue an Http GET request.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">// create the rest client instance
1 RestClient client = new RestClient();
// create the resource instance to interact with
2 Resource resource = client.resource(<span class="code-quote">"http://services.com/HelloWorld"</span>);
// perform a GET on the resource. The resource will be returned as plain text
3 String response = resource.accept(<span class="code-quote">"text/plain"</span>).get(String.class);
</pre>
</div></div>
<h4><a name="6.1GettingStartedwithApacheWinkClient-Explanation"></a>Explanation</h4>
<p>The RestClient is the entry point for building a RESTful Web Service client. In order to start working with the Wink Client, a new instance of RestClient needs to be created, as the example shows in line 1 of the example. A new Resource is then created with the given URI, by calling the RestClient#resource() method as appears in line 2.</p>
<p>Finally, the Resource#get() method is invoked in order to issue an Http GET request as appears in line 3.<br/>
Once the Http response is returned, the client invokes the relevant provider to desterilizes the response in line 3.</p>
<h3><a name="6.1GettingStartedwithApacheWinkClient-POSTRequest"></a>POST Request</h3>
<p>The following example demonstrates how to issue an Http POST request.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">// create the rest client instance
1 RestClient client = new RestClient();
// create the resource instance to interact with
2 Resource resource = client.resource(<span class="code-quote">"http://services.co"</span>);
// issue the request
3 String response = resource.contentType(<span class="code-quote">"text/plain"</span>).accept(<span class="code-quote">"text/plain"</span>).post(String.class, <span class="code-quote">"foo"</span>);
</pre>
</div></div>
<h4><a name="6.1GettingStartedwithApacheWinkClient-Explanation"></a>Explanation</h4>
<p>The POST Request example demonstrates how to issue a simple Http POST request that sends and receives resources as strings.</p>
<p>First, a new instance of a Resource is created through the RestClient. The Http POST request is then issued by specifying the request and response media types and the response entity type (String.class).</p>
<h3><a name="6.1GettingStartedwithApacheWinkClient-POSTAtomRequest"></a>POST Atom Request</h3>
<p>The following example demonstrates how to issue an Http POST request that sends and receives atom entries.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">// create the rest client instance
1 RestClient client = new RestClient();
// create the resource instance to interact with
2 Resource resource = client.resource(<span class="code-quote">"http://services.co"</span>);
3 AtomEntry request = getAtomEntry();
// issue the request
4 AtomEntry response = resource.contentType(<span class="code-quote">"application/atom+xml"</span>).accept(<span class="code-quote">"application/atom+xml"</span>).post(AtomEntry.class, request);
</pre>
</div></div>
<h4><a name="6.1GettingStartedwithApacheWinkClient-Explanation"></a>Explanation</h4>
<p>The Apache Wink Client provides an object model for Atom (atom feed and atom entry), and supplies out-of-the-box providers that enable sending and receiving atom feeds and entries.</p>
<h3><a name="6.1GettingStartedwithApacheWinkClient-UsingClientResponse"></a>Using ClientResponse</h3>
<p>The following example demonstrates how to use the ClientResponse object in order to de-serialize the response entity.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">// create the rest client instance
1 RestClient client = new RestClient();
// create the resource instance to interact with
2 Resource resource = client.resource(<span class="code-quote">"http://services.co"</span>);
// issue the request
3 ClientResponse response = resource.accept(<span class="code-quote">"text/plain"</span>).get();
// deserialize response
4 String responseAsString = response.getEntity(String.class);
</pre>
</div></div>
<h4><a name="6.1GettingStartedwithApacheWinkClient-Explanation"></a>Explanation</h4>
<p>If the response entity type is not provided when invoking the Resource#get() method that appears in line 3, the response will be returned as the raw ClientResponse. In order to trigger the response deserialization mechanism, the ClientResponse#getEntity() method needs to be invoked as it appears in line 4 with the required response entity type.</p>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td height="12" background="http://cwiki.apache.org/confluence/images/border/border_bottom.gif"><img src="images/border/spacer.gif" width="1" height="1" border="0"/></td>
</tr>
<tr>
<td align="center"><font color="grey">Document generated by Confluence on Nov 11, 2009 06:57</font></td>
</tr>
</table>
</body>
</html>