This tutorial covers how to develop some Java REST client examples using OkHttp, an HTTP & HTTP/2 client for Android and Java applications.

1. Preparation

Let’s assume that we have an RESTful web service with several API as below. And we’re going to use OkHttp client to create several basic Java REST client examples which will communicate with this RESTful web service.

Assume that we have an RESTful web services with several API as below:

1.1 Get all books

Responses: application/json

1.2 Create a new book

Request

Example:

Responses: application/json

Example:

STATUS 201 if the book is created successfully.

1.3 Update a book

Request

Example:

Responses: application/json

STATUS 200 if the book is updated successfully.

STATUS 400 if there is no book with given id

1.4 Delete a book

Responses: application/json

STATUS 204 if the book is deleted successfully.

STATUS 400 if there is no book with given id or can not delete the book.

1.5 Source code

The demo source code can be found on the Github or you can download it here: Java-Examples.zip

Let’s define a POJO to map with the Book response as below:

We use OkHttp client to communicate with the RESTful service above and we use Jackson to convert JSON responses to Java objects and vice versa.

2. Java REST client examples using OkHttp

In this section, we’re going to use OkHttp library to create, update, query, and delete resources from REST API.

2.1. Make a HTTP GET request to the RESTful web service (Get all books)

At first, we need to create an OkHttpClient object and create a Request with the URL is our REST API. After the client executes the request, we get back the response which is in JSON format and will be converted to Java objects by using Jackson.

2.2. Make a HTTP POST request to the RESTful web service (create a book)

In this example, before creating request, we have to convert the Book which we want to created, into JSON to match with the requirement of REST API. We also need to set the content type of an HTTP request as application/json. In similar to the HTTP GET request, after executing the request, we get back to response and use Jackson convert it into a Java object.

2.3. Make a HTTP PUT request to the RESTful web service (Update a book)

In this example, we have just issued an HTTP PUT request to the REST API. In similar to the HTTP POST above, we have to specify clearly the HTTP method that the REST API required. Before sending the request we have to convert the Book to JSON and after getting back the response, we convert it to a Java object.

2.4. Make a HTTP DELETE request to the RESTful web service (delete a book)

As required from the REST API, to delete the book, we have to issue an HTTP DELETE method to the resource URI.

4. Conclusions

We have just implemented several Java REST client examples using OkHttp library, an HTTP & HTTP/2 client for Android and Java applications powered by Square. OkHttp is introduced as a very efficiency Http client with HTTP/2 support or connection pooling reduces request latency, etc. In future posts, we will explore more feature of this library. Meanwhile, there are other tutorials related to the Java REST client for your references:

OkHttp Post Examples

Java REST Client Example With Retrofit 2

Java REST Client Using Netflix Feign

Java REST Client Using Apache HttpClient

Java REST Client Using Spring RestTemplate

Simple Java REST Client

Java REST Client With Jersey Client

Java REST Client Using Resteasy Client

Java REST Client Using Resteasy Client Proxy Framework

Java REST Client Using Apache CXF Proxy based API

Basic Authentication with OkHttp Example

Download a File with OkHttp

Upload a File with OkHttp

Set Timeout with OkHttp

WebSocket Client Example with OkHttp

How to Cache Response with OkHttp

0 0 vote
Article Rating