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

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

{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 when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

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 DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}