Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

REST Service Client with JAX-RS API

DZone's Guide to

REST Service Client with JAX-RS API

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

{Editor's Note: Thanks to Thamizh Arasu, a new MVB at DZone. We're pleased to have him on board as a Most Valuable Blogger. Check out his blog, H2 Labz, here.}

Background:

Are you still using Apache HTTP client or Java URL connection to speak with REST service? Then this article is for you. JAX-RS specification has released with new client API to communicate with RESTful services. Here you can see how you can use JAX-RS client API for your communication.

As a first step, we have to create a web target in-order to talk to our web services. Using the ClientBuilder, we should create a Client object.

From the client object we can create WebTarget object like below,

WebTarget api will accept base path and resource path separately. But we can also give the combined url as a whole to the target method.

Once we get the webTarget object, we are ready to communicate with the REST service. From the webTarget object we can invoke the respective REST methods...

In the above image we have called the get method. This will return the response object. From the response object we can validate the response status and read the entity from the response.

Just to get the JSON string from the response, we have passed the object type (String.class) to the readEntity method. If we get any complex object type, we can still use them.

I hope you will enjoy this article. Please give your comments or feedback if you have any... We will meet shortly with another article.



Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:

Published at DZone with permission of Thamizh Arasu, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}