<div dir="ltr" style="text-align: justify;">
<p>This tutorial show you how to use Jersey client APIs to create a RESTful Java client to perform “GET” requests to REST service that created in this <b><a href="https://dineshonjava.com/restful-web-services-with-jersey-jax-rs/" target="_blank" rel="noopener">How to build RESTful Service with Java using JAX-RS and Jersey (Example) example</a></b>.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/RESTful-Service-Client-Example-Crunchify-Tutorial.png" border="0" /></div>
<p> ;</p>
<pre class="highlight">package com.dineshonjava.ws.rest.client; 
 
import com.sun.jersey.api.client.Client; 
import com.sun.jersey.api.client.ClientResponse; 
import com.sun.jersey.api.client.WebResource; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class RestWebServiceClient { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 RestWebServiceClient client = new RestWebServiceClient(); 
 client.sayHello(); 
 client.getMessage("Welcome"); 
 client.getXMLEmployees(); 
 client.getXMLEmployee(11111); 
 client.getJSONEmployees(); 
 client.getJSONEmployee(11111); 
 } 
 
 private void sayHello() { 
 try { 
 Client client = Client.create(); 
 WebResource webResource = client.resource("http://localhost:8181/sdnext/doj/webservice/hello"); 
 ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class); 
 if (response.getStatus() != 200) { 
 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
 } 
 
 String output = response.getEntity(String.class); 
 System.out.println("n============getResponse============"); 
 System.out.println(output); 
 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 } 
 
 private void getMessage(String msg) { 
 try { 
 Client client = Client.create(); 
 WebResource webResource = client.resource("http://localhost:8181/sdnext/doj/webservice/message/"+msg); 
 ClientResponse response = webResource.accept("text/plain").get(ClientResponse.class); 
 if (response.getStatus() != 200) { 
 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
 } 
 
 String output = response.getEntity(String.class); 
 System.out.println("n============getCResponse============"); 
 System.out.println(output); 
 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 } 
 
 private void getXMLEmployees() { 
 try { 
 Client client = Client.create(); 
 WebResource webResource = client.resource("http://localhost:8181/sdnext/doj/webservice/employees"); 
 ClientResponse response = webResource.accept("application/xml").get(ClientResponse.class); 
 if (response.getStatus() != 200) { 
 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
 } 
 
 String output = response.getEntity(String.class); 
 System.out.println("n============getCResponse============"); 
 System.out.println(output); 
 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 } 
 private void getXMLEmployee(int empid) { 
 try { 
 Client client = Client.create(); 
 WebResource webResource = client.resource("http://localhost:8181/sdnext/doj/webservice/employee/"+empid); 
 ClientResponse response = webResource.accept("application/xml").get(ClientResponse.class); 
 if (response.getStatus() != 200) { 
 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
 } 
 
 String output = response.getEntity(String.class); 
 System.out.println("n============getCResponse============"); 
 System.out.println(output); 
 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 } 
 private void getJSONEmployees() { 
 try { 
 Client client = Client.create(); 
 WebResource webResource = client.resource("http://localhost:8181/sdnext/doj/webservice/json/employees/"); 
 ClientResponse response = webResource.accept("application/json").get(ClientResponse.class); 
 if (response.getStatus() != 200) { 
 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
 } 
 
 String output = response.getEntity(String.class); 
 System.out.println("n============getCResponse============"); 
 System.out.println(output); 
 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 } 
 private void getJSONEmployee(int empid) { 
 try { 
 Client client = Client.create(); 
 WebResource webResource = client.resource("http://localhost:8181/sdnext/doj/webservice/json/employee/"+empid); 
 ClientResponse response = webResource.accept("application/json").get(ClientResponse.class); 
 if (response.getStatus() != 200) { 
 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
 } 
 
 String output = response.getEntity(String.class); 
 System.out.println("n============getCResponse============"); 
 System.out.println(output); 
 
 } catch (Exception e) { 
 e.printStackTrace(); 
 } 
 } 
} 
 
</pre>
<div id="ads-id" align="center"></div>
<p>output:</p>
<div style="background-color: #ff99cc;">
<p>============getResponse============<br />
Hello World!!! dineshonjava</p>
<p>============getCResponse============<br />
Welcome</p>
<p>============getCResponse============<br />
<;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243; standalone=&#8221;yes&#8221;?>;<br />
<;employees>;<br />
<;employee>;<br />
<;address>;New Delhi<;/address>;<br />
<;employeeId>;22222<;/employeeId>;<br />
<;employeeName>;Abhishek<;/employeeName>;<br />
<;jobType>;Marketing<;/jobType>;<br />
<;salary>;50000<;/salary>;<br />
<;/employee>;<br />
<;employee>;<br />
<;address>;Noida<;/address>;<br />
<;employeeId>;11111<;/employeeId>;<br />
<;employeeName>;Dineh Rajput<;/employeeName>;<br />
<;jobType>;Sr.Software Engineer<;/jobType>;<br />
<;salary>;70000<;/salary>;<br />
<;/employee>;<br />
<;/employees>;</p>
<p>============getCResponse============<br />
<;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243; standalone=&#8221;yes&#8221;?>;<br />
<;employee>;<br />
<;address>;Noida<;/address>;<br />
<;employeeId>;11111<;/employeeId>;<br />
<;employeeName>;Dineh Rajput<;/employeeName>;<br />
<;jobType>;Sr.Software Engineer<;/jobType>;<br />
<;salary>;70000<;/salary>;<br />
<;/employee>;</p>
<p>============getCResponse============<br />
{&#8220;employee&#8221;:[{&#8220;address&#8221;:&#8221;New Delhi&#8221;,&#8221;employeeId&#8221;:&#8221;22222&#8243;,&#8221;employeeName&#8221;:&#8221;Abhishek&#8221;,&#8221;jobType&#8221;:&#8221;Marketing&#8221;,&#8221;salary&#8221;:&#8221;50000&#8243;},{&#8220;address&#8221;:&#8221;Noida&#8221;,&#8221;employeeId&#8221;:&#8221;11111&#8243;,&#8221;employeeName&#8221;:&#8221;Dineh Rajput&#8221;,&#8221;jobType&#8221;:&#8221;Sr.Software Engineer&#8221;,&#8221;salary&#8221;:&#8221;70000&#8243;}]}</p>
<p>============getCResponse============<br />
{&#8220;address&#8221;:&#8221;Noida&#8221;,&#8221;employeeId&#8221;:&#8221;11111&#8243;,&#8221;employeeName&#8221;:&#8221;Dineh Rajput&#8221;,&#8221;jobType&#8221;:&#8221;Sr.Software Engineer&#8221;,&#8221;salary&#8221;:&#8221;70000&#8243;}</p>
</div>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/06/jax-rs-client.png" border="0" /></div>
<p><b>Download Source Code + Libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/RESTClient.zip?attredirects=0&;d=1" target="_blank" rel="noopener">RESTClient.zip</a></b></p>
<p><i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank" rel="noopener">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank" rel="noopener">Wikipedia for REST Web Service</a></b></i></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/restful-web-services-with-jersey-jax-rs/">Previous </a><;<; || <a href="https://dineshonjava.com/jax-rs-web-service-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/overview-of-jax-rs-application/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/restful-web-services-with-jersey-jax-rs/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/overview-of-jax-rs-application/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '429'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…