Over a million developers have joined DZone.

REST Service Client with JAX-RS API

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

{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.



Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}