🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this post, we will create a Rest Client using OkHttp library. Let's create a Rest Client for GET, POST, PUT and DELETE HTTP request in Java.
OkHTTP is an open source project designed to be an efficient HTTP client for Android and Java applications.
OkHttp supports Android 5.0+ (API level 21+) and Java 1.8+. In this article, we will write a code using Java 1.8+.
Maven Dependency
Let’s first add the library as a dependency into the pom.xml:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
To see the latest dependency of this library check out the page on Maven Central.
In this post, we will create a Rest Client for spring boot CRUD example project. This spring boot crud example project is deployed and up and running. Refer to this article - https://www.javaguides.net/2018/09/spring-boot-2-hibernate-5-mysql-crud-rest-api-tutorial.html
Let's first do the POST request to insert user object into a database and then we will do GET, PUT and DELETE requests.
OkHttp POST Request Java Example
package com.javaguides.okhttp.tutorial.crud;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OkHttpPost {
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url(url).post(body).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void main(String[] args) throws IOException {
OkHttpPost example = new OkHttpPost();
String json = "{\r\n" +
" \"firstName\" : \"Ramesh\",\r\n" +
" \"lastName\" : \"Fadatare\",\r\n" +
" \"emailId\" : \"ramesh@gmail.com\"\r\n" +
"}";
String response = example.post("http://localhost:8080/api/v1/employees", json);
System.out.println(response);
}
}
Below diagram shows the screenshot of source code as well as output:
OkHttp GET Request Java Example
package com.javaguides.okhttp.tutorial.crud;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpGet {
OkHttpClient client = new OkHttpClient();
public String run(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void main(String[] args) throws IOException {
OkHttpGet example = new OkHttpGet();
String response = example.run("http://localhost:8080/api/v1/employees/1");
System.out.println(response);
}
}
Below diagram shows the screenshot of source code as well as output:
OkHttp PUT Request Java Example
package com.javaguides.okhttp.tutorial.crud;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OkHttpPut {
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url(url).put(body).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void main(String[] args) throws IOException {
OkHttpPut example = new OkHttpPut();
String json = "{\r\n" +
" \"firstName\" : \"Ram\",\r\n" +
" \"lastName\" : \"Fadatare\",\r\n" +
" \"emailId\" : \"ram@gmail.com\"\r\n" +
"}";
String response = example.post("http://localhost:8080/api/v1/employees/1", json);
System.out.println(response);
}
}
Below diagram shows the screenshot of source code as well as output:
OkHttp DELETE Request Java Example
package com.javaguides.okhttp.tutorial.crud;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttpDelete {
OkHttpClient client = new OkHttpClient();
public String run(String url) throws IOException {
Request request = new Request.Builder().url(url).delete().build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void main(String[] args) throws IOException {
OkHttpDelete example = new OkHttpDelete();
String response = example.run("http://localhost:8080/api/v1/employees/1");
System.out.println(response);
}
}
Below diagram shows the screenshot of source code as well as output:
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment