Simplify HTTP Calls in Java with OKHttpUtil: A Complete Guide
This article introduces OKHttpUtil, a lightweight Java HTTP client wrapper, outlines its key features, provides Maven setup and code examples for GET, POST, file upload/download, demonstrates fluent HttpRequest usage, shows Spring Boot integration, and explains how to quickly encapsulate external APIs such as eBay.
OKHttpUtil Overview
In the Java ecosystem, Apache HttpClient has long dominated HTTP client libraries, but its large size and complex API make it less suitable for lightweight scenarios. OKHttp, developed by Square, offers a compact and easy‑to‑use alternative that works in both Java and Kotlin, and is widely adopted in Android apps and server‑side code.
OKHttpUtil Features
Automatically determines HTTP or HTTPS from the URL.
Cookie management for simulated login sessions.
Automatic handling of 304 redirects.
Proxy configuration support.
Referer and User‑Agent configuration.
Gzip response decompression.
Spring Boot configuration file support.
Minimal wrapper for simple calls.
OKHttpUtil Usage
Add the dependency to your Maven project:
<dependency>
<groupId>io.github.admin4j</groupId>
<artifactId>http</artifactId>
<version>0.4.0</version>
</dependency>GET request example:
Response response = HttpUtil.get("https://github.com/search", Pair.of("q", "okhttp"));
System.out.println("response = " + response);POST request with JSON body:
Response post = HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=...", "{\"msgtype\":\"text\",\"text\":{\"content\":\"...\"}}");
System.out.println("post = " + post);Form POST example, file upload, and file download are also demonstrated with HttpUtil.upload and HttpUtil.down.
HttpRequest Chain Requests
Fluent API for building requests:
Response response = HttpRequest.get("https://search.gitee.com/?skin=rec&type=repository")
.queryMap("q", "admin4j")
.header(HttpHeaderKey.USER_AGENT, "admin4j")
.execute();
System.out.println("response = " + response);Integration with Spring Boot
Use the starter common-http-starter:
<dependency>
<groupId>io.github.admin4j</groupId>
<artifactId>common-http-starter</artifactId>
<version>0.4.0</version>
</dependency>Custom configuration can be provided by defining a bean similar to the shown HttpConfig class, where you can set logging level, timeouts, proxy, cookie support, etc.
Quickly Wrapping External APIs
Example of encapsulating eBay APIs with a base client class and specific inventory and order clients. The code shows how to set headers, build query maps, and call the eBay endpoints.
public class EbayClient extends ApiJsonClient {
public EbayClient(Long storeId) {
// configure basePath and default headers
}
}Inventory client and order client extend this base class and expose methods such as inventoryItem and orders that return JSONObject results.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
