GP Coder

Trang chia sẻ kiến thức lập trình Java

  • Java Core
    • Basic Java
    • OOP
    • Exception Handling
    • Multi-Thread
    • Java I/O
    • Networking
    • Reflection
    • Collection
    • Java 8
  • Design pattern
    • Creational Pattern
    • Structuaral Pattern
    • Behavior Pattern
  • Web Service
    • SOAP
    • REST
  • JPA
  • Java library
    • Report
    • Json
    • Unit Test
  • Message Queue
    • ActiveMQ
    • RabbitMQ
  • All
Trang chủ Java Webservice REST Tạo ứng dụng Java RESTful Client với thư viện OkHttp

Tạo ứng dụng Java RESTful Client với thư viện OkHttp

Đăng vào 15/07/2019 . Được đăng bởi GP Coder . 11010 Lượt xem . Toàn màn hình

Trong các bài viết trước chúng ta sử dụng thư viện Jersey client để gọi các RESTful API. Trong bài này, tôi sẽ giới thiệu với các bạn thư viện khác, rất mạnh mẽ để gọi các RESTful API là OkHttp.

Nội dung

  • 1 Giới thiệu OkHttp
  • 2 Ví dụ CRUD Restful Client với OkHttp

Giới thiệu OkHttp

OkHttp là một thư viện Java mã nguồn mở, được thiết kế để trở thành một HTTP Client hiệu quả với tốc độ tải nhanh và giảm băng thông đường truyền.

Một số đặc điểm nỗi bật của OkHttp:

  • Hỗ trợ Http/2 protocol.
  • Connection pooling : giúp giảm độ trợ của các request.
  • GZIP compression : hỗ trợ nén dữ liệu khi truyền tải.
  • Cache : tránh lặp lại một request nhiều lần.
  • Hỗ trợ synchronous và asynchronous.

OkHttp được sử dụng để gửi và nhận kết quả từ các ứng dụng Restful web service.

Một số thành phần của OkHttp:

  • Request.Builder : hỗ trợ tạo request bao gồm : HTTP Method, header, cookie, media type, …
  • RestClient : chịu trách nhiệm giao tiếp với REST service bao gồm gửi Request và nhận Response.

Ví dụ CRUD Restful Client với OkHttp

Tạo project

Tạo maven project và khai báo dependency sau trong file pom.xml.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.gpcoder</groupId>
	<artifactId>RestfulClientWithOkHttpExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>RestfulClientWithOkHttpExample</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<lombok.version>1.16.20</lombok.version>
	</properties>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
		<dependency>
			<groupId>com.squareup.okhttp3</groupId>
			<artifactId>okhttp</artifactId>
			<version>3.14.2</version>
		</dependency>
	</dependencies>
</project>

Tạo CRUD Restful Client với OkHttp

Trong ví dụ này, chúng ta sẽ gọi lại các Restful API chúng ta đã tạo ở bài viết trước “JWT – Token-based Authentication trong Jersey 2.x“.

Đầu tiên, chúng ta cần gọi API /auth để lấy token và sau đó chúng ta sẽ attach token này vào mỗi request để truy cập resource.

AuthenticationClient.java


package com.gpcoder.service;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * This is a first step in restful flow. We must authorize first and use the token for other request.
 */
public class AuthenticationClient {

	public static final String AUTH_URL = "http://localhost:8080/RestfulWebServiceExample/rest/auth";
	public static final String TOKEN = getToken();
	
	/**
	 * Get Json Web Token (JWT)
	 * 
	 * @POST http://localhost:8080/RestfulWebServiceExample/rest/auth
	 */
	private static String getToken() {
		RequestBody requestBody = new FormBody.Builder()
		        .add("username", "gpcoder")
		        .add("password", "gpcoder")
		        .build();

		Request request = new Request.Builder()
		        .url(AUTH_URL)
		        .post(requestBody)
		        .build();
		
		OkHttpClient client = new OkHttpClient();
		try (Response response = client.newCall(request).execute()) {
			if (!response.isSuccessful()) {
				throw new IOException("Unexpected code: " + response);
			}
			String token = response.body().string();
			System.out.println("Token: " + token);
			return token;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

OkHttpClientExample.java


package com.gpcoder;

import java.io.IOException;

import com.gpcoder.service.AuthenticationClient;

import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpClientExample {

	public static final String API_URL = "http://localhost:8080/RestfulWebServiceExample/rest/orders";
	public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
	private static OkHttpClient client;
	
	public static void main(String[] args) throws IOException {
		client = new OkHttpClient();
		String token = AuthenticationClient.TOKEN;
		createOrder(token);
		retrieveOrder(token);
		updateOrder(token);
		deleteOrder(token);
	}
	
	/**
	 * @POST http://localhost:8080/RestfulWebServiceExample/rest/orders
	 */
	private static void createOrder(String token) throws IOException {
		RequestBody requestBody = RequestBody.create(JSON, createJsonData());
		
		Request request = new Request.Builder()
				.header("Authorization", "Bearer " + token)
				.url(API_URL)
				.post(requestBody)
				.build();

		try (Response response = client.newCall(request).execute()) {
			String result = response.body().string();
			System.out.println("createOrder: " + result);
		}
	}
	
	/**
	 * @GET http://localhost:8080/RestfulWebServiceExample/rest/orders/1
	 */
	private static void retrieveOrder(String token) throws IOException {
		HttpUrl.Builder urlBuilder = HttpUrl.parse(API_URL).newBuilder();
		urlBuilder.addPathSegment("1");
		String url = urlBuilder.build().toString();
		
		Request request = new Request.Builder()
				.header("Authorization", "Bearer " + token)
				.url(url)
				.build();

		try (Response response = client.newCall(request).execute()) {
			String result = response.body().string();
			System.out.println("retrieveOrder: " + result);
		}
	}
	
	/**
	 * @PUT http://localhost:8080/RestfulWebServiceExample/rest/orders
	 */
	private static void updateOrder(String token) throws IOException {
		RequestBody requestBody = RequestBody.create(JSON, createJsonData());
		
		Request request = new Request.Builder()
				.header("Authorization", "Bearer " + token)
				.url(API_URL)
				.post(requestBody)
				.build();

		try (Response response = client.newCall(request).execute()) {
			String result = response.body().string();
			System.out.println("updateOrder: " + result);
		}
	}
	
	/**
	 * @DELETE http://localhost:8080/RestfulWebServiceExample/rest/orders/1
	 */
	private static void deleteOrder(String token) throws IOException {
		HttpUrl.Builder urlBuilder = HttpUrl.parse(API_URL).newBuilder();
		urlBuilder.addPathSegment("1");
		String url = urlBuilder.build().toString();
		
		Request request = new Request.Builder()
				.header("Authorization", "Bearer " + token)
				.url(url)
				.delete()
				.build();

		try (Response response = client.newCall(request).execute()) {
			String result = response.body().string();
			System.out.println("deleteOrder: " + result);
		}
	}
	
	private static String createJsonData() {
		return "{\"id\": 1, \"name\": \"gpcoder\"}";
	}
}

Sử dụng Interceptor với OkHttp

Interceptor là một tính năng rất hữu ích nó giúp chúng ta có thể chỉnh sửa request/response, retry call, monitor ứng dụng.

OkHttp hỗ trợ 2 loại Interceptor:

  • Application Interceptor : loại interceptor này thường được sử dụng để thay đổi headers/query cho cả request/ response. Loại interceptor này chắc chắn được gọi 1 lần ngay cả khi nó được truy xuất từ bộ nhớ đệm (Cache).
  • Network Interceptor : được sử dụng để giám sát yêu cầu giống như được truyền qua mạng. Nó rất hữu ích để theo dõi chuyển hướng (redirect) , thử lại (retry) và cung cấp quyền truy cập vào các resource của request. Loại interceptor này sẽ KHÔNG được gọi nếu nó được truy xuất từ bộ nhớ đệm (Cache).

Bây giờ chúng ta sẽ sử dụng Interceptor để tự động thêm Token vào mỗi request, chúng ta không cần thêm nó một cách thủ công trong từng request. Ví dụ bên dưới sử dụng 2 Interceptor:

  • AuthInterceptor : thêm Token vào mỗi request.
  • LoggingInterceptor : log trước khi gửi request và sau khi nhận response.

AuthInterceptor.java


package com.gpcoder.interceptor;

import java.io.IOException;

import com.gpcoder.service.AuthenticationClient;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class AuthInterceptor implements Interceptor {

	@Override
	public Response intercept(Chain chain) throws IOException {
		/*
		 * chain.request() returns original request that you can work with(modify,
		 * rewrite)
		 */
		Request originalRequest = chain.request();

		// Here we can rewrite the request
		// We add an Authorization header if the request is not an authorize request and already had a token
		Request authRequest = originalRequest;
		if (!originalRequest.url().toString().contains("/auth") && AuthenticationClient.TOKEN != null) {
			authRequest = originalRequest.newBuilder()
					.header("Authorization", "Bearer " + AuthenticationClient.TOKEN)
			        .build();
		}
		
		/*
		 * chain.proceed(request) is the call which will initiate the HTTP work. This
		 * call invokes the request and returns the response as per the request.
		 */
		Response response = chain.proceed(authRequest);
		
		// Here we can rewrite/modify the response
		
		return response;
	}
}

Tương tự chúng ta sẽ tạo LoggingInterceptor
LoggingInterceptor.java


package com.gpcoder.interceptor;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class LoggingInterceptor implements Interceptor {
	@Override
	public Response intercept(Interceptor.Chain chain) throws IOException {
		Request request = chain.request();

		long t1 = System.nanoTime();
		System.out.println(
				String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers()));

		Response response = chain.proceed(request);

		long t2 = System.nanoTime();
		System.out.println(String.format("Received response for %s in %.1fms%n%s", response.request().url(),
				(t2 - t1) / 1e6d, response.headers()));

		return response;
	}
}

Để sử dụng Interceptor, chúng ta cần đăng ký với Client thông qua phương thức addInterceptor() hoặc addNetworkInterceptor(). Chương trình bên dưới, tôi đăng ký AuthInterceptor ở mức Application và LoggingInterceptor cho cả 2 mức Application và Network.


package com.gpcoder;

import java.io.IOException;

import com.gpcoder.interceptor.AuthInterceptor;
import com.gpcoder.interceptor.LoggingInterceptor;

import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpClientWithInterceptorExample {

	public static final String API_URL = "http://localhost:8080/RestfulWebServiceExample/rest/orders";
	public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
	private static OkHttpClient client;
	
	public static void main(String[] args) throws IOException {
		client = new OkHttpClient.Builder()
				.addInterceptor(new LoggingInterceptor())
			    .addInterceptor(new AuthInterceptor())
			    .addNetworkInterceptor(new LoggingInterceptor())
			    .build();
		
		createOrder();
		retrieveOrder();
		updateOrder();
		deleteOrder();
	}
	
	/**
	 * @POST http://localhost:8080/RestfulWebServiceExample/rest/orders
	 */
	private static void createOrder() throws IOException {
		RequestBody requestBody = RequestBody.create(JSON, createJsonData());
		
		Request request = new Request.Builder()
				.url(API_URL)
				.post(requestBody)
				.build();

		try (Response response = client.newCall(request).execute()) {
			String result = response.body().string();
			System.out.println("createOrder: " + result);
		}
	}
	
	/**
	 * @GET http://localhost:8080/RestfulWebServiceExample/rest/orders/1
	 */
	private static void retrieveOrder() throws IOException {
		HttpUrl.Builder urlBuilder = HttpUrl.parse(API_URL).newBuilder();
		urlBuilder.addPathSegment("1");
		String url = urlBuilder.build().toString();
		
		Request request = new Request.Builder()
				.url(url)
				.build();

		try (Response response = client.newCall(request).execute()) {
			String result = response.body().string();
			System.out.println("retrieveOrder: " + result);
		}
	}
	
	/**
	 * @PUT http://localhost:8080/RestfulWebServiceExample/rest/orders
	 */
	private static void updateOrder() throws IOException {
		RequestBody requestBody = RequestBody.create(JSON, createJsonData());
		
		Request request = new Request.Builder()
				.url(API_URL)
				.post(requestBody)
				.build();

		try (Response response = client.newCall(request).execute()) {
			String result = response.body().string();
			System.out.println("updateOrder: " + result);
		}
	}
	
	/**
	 * @DELETE http://localhost:8080/RestfulWebServiceExample/rest/orders/1
	 */
	private static void deleteOrder() throws IOException {
		HttpUrl.Builder urlBuilder = HttpUrl.parse(API_URL).newBuilder();
		urlBuilder.addPathSegment("1");
		String url = urlBuilder.build().toString();
		
		Request request = new Request.Builder()
				.url(url)
				.delete()
				.build();

		try (Response response = client.newCall(request).execute()) {
			String result = response.body().string();
			System.out.println("deleteOrder: " + result);
		}
	}
	
	private static String createJsonData() {
		return "{\"id\": 1, \"name\": \"gpcoder\"}";
	}
}

Chạy ứng dụng, các bạn sẽ thấy nó có cùng kết quả với ví dụ đầu tiên. Tuy nhiên, chúng ta không cần quan tâm đến token nữa. Mọi thứ đã được handle trong AuthInterceptor.

Trên đây là những thông tin cơ bản về OkHttp, ngoài ra chúng ta có thể upload, download file khi sử dụng retrofit. Trong java thường nếu xử lý trên web thì ít khi dùng đến thư viện OkHttp, thư viện này được sử dụng chủ yếu trong các ứng dụng Android để thao tác từ client đến server. Hy vọng bài viết giúp ích cho các bạn, hẹn gặp lại ở các bài viết tiếp theo.

Tài liệu tham khảo:

  • http://square.github.io/okhttp
  • https://www.baeldung.com/guide-to-okhttp
5.0
05
Nếu bạn thấy hay thì hãy chia sẻ bài viết cho mọi người nhé! Và Donate tác giả

Shares

Chuyên mục: Java library, Java Webservice, REST Được gắn thẻ: Interceptor, REST, Webservice

Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Tạo ứng dụng Java RESTful Client với thư viện Retrofit

Có thể bạn muốn xem:

  • REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x (13/06/2019)
  • Giới thiệu Google Guice – Injection, Scope (07/02/2019)
  • Giới thiệu thư viện phổ biến của java – Project Lombok (09/04/2018)
  • Giới thiệu Castle Mock – Mock REST APIs và SOAP web-services (05/09/2019)
  • Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman (10/06/2019)

Bình luận

bình luận

Tìm kiếm

Bài viết mới

  • Clean code 13/01/2024
  • Giới thiệu CloudAMQP – Một RabbitMQ server trên Cloud 02/10/2020
  • Kết nối RabbitMQ sử dụng Web STOMP Plugin 19/06/2020
  • Sử dụng publisher confirm trong RabbitMQ 16/06/2020
  • Sử dụng Dead Letter Exchange trong RabbitMQ 13/06/2020

Xem nhiều

  • Hướng dẫn Java Design Pattern – Factory Method (97349 lượt xem)
  • Hướng dẫn Java Design Pattern – Singleton (96986 lượt xem)
  • Giới thiệu Design Patterns (86646 lượt xem)
  • Lập trình đa luồng trong Java (Java Multi-threading) (85481 lượt xem)
  • Giới thiệu về Stream API trong Java 8 (83023 lượt xem)

Nội dung bài viết

  • 1 Giới thiệu OkHttp
  • 2 Ví dụ CRUD Restful Client với OkHttp

Lưu trữ

Thẻ đánh dấu

Annotation Authentication Basic Java Behavior Pattern Collection Creational Design Pattern Cấu trúc điều khiển Database Dependency Injection Design pattern Eclipse Exception Executor Service Google Guice Gson Hibernate How to Interceptor IO Jackson Java 8 Java Core JDBC JDK Jersey JMS JPA json JUnit JWT Message Queue Mockito Multithreading OOP PowerMockito RabbitMQ Reflection Report REST SOAP Structuaral Pattern Swagger Thread Pool Unit Test Webservice

Liên kết

  • Clean Code
  • JavaTpoint
  • Refactoring Guru
  • Source Making
  • TutorialsPoint
  • W3Schools Online Web Tutorials

Giới thiệu

GP Coder là trang web cá nhân, được thành lập với mục đích lưu trữ, chia sẽ kiến thức đã học và làm việc của tôi. Các bài viết trên trang này chủ yếu về ngôn ngữ Java và các công nghệ có liên quan đến Java như: Spring, JSF, Web Services, Unit Test, Hibernate, SQL, ...
Hi vọng góp được chút ít công sức cho sự phát triển cộng đồng Coder Việt.

Donate tác giả

Tìm kiếm các bài viết của GP Coder với Google Search

Liên hệ

Các bạn có thể liên hệ với tôi thông qua:
  • Trang liên hệ
  • Linkedin: gpcoder
  • Email: contact@gpcoder.com
  • Skype: ptgiang56it

Follow me

Copyright 2025 © GP Coder · All Rights Reserved · Giới thiệu · Chính sách · Điều khoản · Liên hệ ·

Share

Blogger
Delicious
Digg
Email
Facebook
Facebook messenger
Flipboard
Google
Hacker News
Line
LinkedIn
Mastodon
Mix
Odnoklassniki
PDF
Pinterest
Pocket
Print
Reddit
Renren
Short link
SMS
Skype
Telegram
Tumblr
Twitter
VKontakte
wechat
Weibo
WhatsApp
X
Xing
Yahoo! Mail

Copy short link

Copy link