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
okhttpthat 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
<code><dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-http</artifactId>
<version>2.7.18.1</version>
</dependency></code>2.2 Quick Example
<code>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);</code>2.3 Asynchronous Request
<code>// 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 request</code>2.4 API Overview
<code>// 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.</code>2.5 Global Configuration
Global Log Configuration:
<code>// 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);</code>Custom OkHttpClient
<code>OkHttpClient httpClient = new OkHttpClient.Builder()
.build();
HttpRequest.setHttpClient(httpClient);</code>2.6 Cookie Management
The library includes an
InMemoryCookieManagerthat automatically stores and uses cookies; you can also provide a custom OkHttp
CookieJar.
<code>InMemoryCookieManager cookieManager = new InMemoryCookieManager();
HttpRequest.get("http://fast.dreamlu.net/api/auth/captcha")
.cookieManager(cookieManager)
.execute()
.asString();</code>3. Performance Highlights
HTTP Benchmark Results
Round 1
<code>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/min</code>Round 2
<code>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/min</code>Round 3
<code>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/min</code>Interpretation
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-resourcesto 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
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.