Boost Your Java HTTP Calls with mica-http: Fast, Fluent, and Secure
This article introduces mica-http, a lightweight Fluent‑style HTTP client built on OkHttp, walks through dependency setup, synchronous and asynchronous usage, global logging, custom client and cookie management, presents performance benchmarks, and highlights its automatic resource handling for safe, high‑performance requests.
1. Introduction
mica-http is a wrapper built on okhttp that provides a Fluent‑style HTTP utility package. It offers simple syntax, better performance, higher security through automatic resource closing, and built‑in request logging.
2. Quick Start
2.1 Import Dependency
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-http</artifactId>
<version>2.7.18.1</version>
</dependency>2.2 Quick Example
String html = HttpRequest.get("https://www.baidu.com")
.useConsoleLog() // log, alternatives: useSlf4jLog() or useLog(log::info)
.execute() // creates the request object
.asString(); // executes and returns the result
System.out.println(html);2.3 Asynchronous Request
// Send asynchronous request
HttpRequest.delete("https://www.baidu.com")
.async()
.onFailed((request, e) -> {
e.printStackTrace();
})
.onResponse(responseSpec -> {
int httpCode = responseSpec.code();
})
.onSuccessful(responseSpec -> {
JsonNode jsonNode = responseSpec.asJsonNode();
})
.execute(); // finally send the request2.4 API Overview
// Synchronous request, supports get, post, patch, put, delete
HttpRequest.get("https://www.baidu.com/{param1}/{param2}")
.useSlf4jLog()
.pathParam("param1", "abc1")
.pathParam("param2", "abc2")
.addHeader("x-account-id", "mica001")
.addCookie(builder -> builder.domain("www.baidu.com").name("name").value("value"))
.query("q", "mica")
.queryEncoded("name", "encodedValue")
.retryOn(responseSpec -> !responseSpec.isOk())
.proxy(InetSocketAddress.createUnresolved("127.0.0.1", 8080))
.formBuilder()
.add("id", 123123)
.execute()
.asJsonNode(); // other converters: asString, asBytes, etc.2.5 Global Configuration
Global Log Configuration:
// Set global log level: NONE, BASIC, HEADERS, BODY
HttpRequest.setGlobalLog(LogLevel.BODY);
// Console logger for SDKs without a logging dependency
HttpRequest.setGlobalLog(HttpLogger.Console, LogLevel.BODY);
// Custom logger
HttpRequest.setGlobalLog(log::info);Custom OkHttpClient
OkHttpClient httpClient = new OkHttpClient.Builder()
.build();
HttpRequest.setHttpClient(httpClient);2.6 Cookie Management
The library includes an InMemoryCookieManager that automatically stores and uses cookies; you can also provide a custom OkHttp CookieJar.
InMemoryCookieManager cookieManager = new InMemoryCookieManager();
HttpRequest.get("http://fast.dreamlu.net/api/auth/captcha")
.cookieManager(cookieManager)
.execute()
.asString();3. Performance Highlights
HTTP Benchmark Results
Round 1
Benchmark Mode Cnt Score Error Units
MicaHttpBenchmark.micaHttp thrpt 5 899.299 ± 208.080 ops/min
MicaHttpBenchmark.okHttp thrpt 5 841.669 ± 106.094 ops/min
MicaHttpBenchmark.protoTypeOkHttp thrpt 5 346.647 ± 23.664 ops/minRound 2
Benchmark Mode Cnt Score Error Units
MicaHttpBenchmark.micaHttp thrpt 5 876.651 ± 276.569 ops/min
MicaHttpBenchmark.okHttp thrpt 5 899.365 ± 194.188 ops/min
MicaHttpBenchmark.protoTypeOkHttp thrpt 5 341.028 ± 34.713 ops/minRound 3
Benchmark Mode Cnt Score Error Units
MicaHttpBenchmark.micaHttp thrpt 5 944.017 ± 175.509 ops/min
MicaHttpBenchmark.okHttp thrpt 5 875.143 ± 164.594 ops/min
MicaHttpBenchmark.protoTypeOkHttp thrpt 5 331.370 ± 19.136 ops/minInterpretation
mica-http enhances okhttp by reusing the OkHttpClient and connection pool, showing no noticeable performance loss and occasionally outperforming raw okhttp.
protoTypeOkHttp creates a new OkHttpClient for each request, causing significant overhead and potential memory leaks under high concurrency.
4. Safety
mica-http uses try-with-resources to automatically close streams, eliminating the need for manual resource management.
5. Conclusion
Many developers have adopted mica-http for handling numerous interfaces; give it a try if you need a fast, fluent, and secure HTTP client. Source code: https://gitee.com/596392912/mica/tree/2.7.x/mica-http
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.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
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.
