Unlock Java Productivity: Master Hutool’s Powerful Utility Library
This article introduces the Hutool Java utility library, outlines its modules, shows how to add it to a Maven project, and provides practical code examples for HTTP requests, random codes, pinyin conversion, timing, number handling, data masking, email sending, and Bloom filters, demonstrating how Hutool can streamline backend development.
In daily development we often use utility classes to speed up Java projects, and Hutool is a popular one in China.
Hutool is a small yet comprehensive Java utility library that encapsulates APIs via static methods, reducing learning cost and making Java feel as elegant as functional languages.
It covers many aspects of low‑level Java code and serves both large projects for solving small problems and small projects for efficiency.
1. Modules Included
Hutool provides the following modules:
hutool-aop : JDK dynamic proxy encapsulation, provides AOP support without IoC.
hutool-bloomFilter : Bloom filter implementation with various hash algorithms.
hutool-cache : Simple cache implementation.
hutool-core : Core utilities including bean operations, date handling, and common utils.
hutool-cron : Scheduling module supporting Crontab expressions.
hutool-crypto : Encryption and decryption utilities for symmetric, asymmetric, and digest algorithms.
hutool-db : JDBC wrapper based on ActiveRecord concepts.
hutool-dfa : Multi‑keyword search based on DFA model.
hutool-extra : Extensions for third‑party integrations such as template engines, mail, servlet, QR code, Emoji, FTP, and word segmentation.
hutool-http : Http client wrapper based on HttpUrlConnection.
hutool-log : Automatic log implementation façade.
hutool-script : Script execution wrapper (e.g., JavaScript).
hutool-setting : Enhanced configuration file and Properties handling.
hutool-system : System parameter access (JVM info, etc.).
hutool-json : JSON utilities.
hutool-captcha : Image captcha implementation.
hutool-poi : POI wrappers for Excel and Word.
hutool-socket : Socket wrapper based on Java NIO/AIO.
hutool-jwt : JSON Web Token (JWT) implementation.
2. Adding the Framework
Add the following dependency to
pom.xml:
<code><dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency></code>3. Common Use Cases
3.1 HTTP Request
Hutool‑http wraps
HttpURLConnection, simplifying HTTPS requests, file upload, and cookie handling.
GET request example:
<code>// GET request
String content = HttpUtil.get(url);</code>POST request example:
<code>// POST request
HashMap<String, Object> param = new HashMap<>();
param.put("city", "Xi'an");
String result = HttpUtil.post("www.javacn.site", param);</code>3.2 Generating Random Verification Codes
<code>// Generate a 4‑digit random verification code
String verificationCode = RandomUtil.randomStringUpper(4);</code>3.3 Pinyin Utilities
Hutool provides a façade for several pinyin libraries (TinyPinyin, JPinyin, Pinyin4j) and automatically selects the available one.
Example to get full pinyin:
<code>// Output: ni hao
String pinyin = PinyinUtil.getPinyin("你好", " ");</code>Example to get first letters:
<code>// Output: h, s, d, y, g
String result = PinyinUtil.getFirstLetter("H是第一个", ", ");</code>3.4 Timer
TimeIntervaloffers simple timing of code execution.
<code>TimeInterval timer = DateUtil.timer();
// ... business logic
long ms = timer.interval(); // elapsed milliseconds
timer.intervalRestart(); // get elapsed time and reset</code>3.5 Number Utilities
NumberUtilprovides rounding, formatting, validation, and random number generation.
<code>double te1 = 123456.123456;
double te2 = 123456.128456;
Console.log(round(te1, 4)); // 123456.1235
Console.log(round(te2, 4)); // 123456.1285</code>Formatting example:
<code>long c = 299792458;
String format = NumberUtil.decimalFormat(",###", c); // 299,792,458</code>3.6 Data Desensitization
Hutool offers utilities to mask sensitive data such as ID cards, phone numbers, passwords, etc.
<code>// Mask ID card number, keep first and last two characters
DesensitizedUtil.idCardNum("51343620000320711X", 1, 2);</code> <code>// Mask phone number
DesensitizedUtil.mobilePhone("18049531999");</code>3.7 Email Sending
MailUtilwraps JavaMail for simple email sending.
<code>// Send a plain‑text email
MailUtil.send("[email protected]", "Test", "Email from Hutool", false);</code>3.8 Bloom Filter
Hutool includes a Bloom filter implementation for fast set membership tests.
<code>// Initialize and use a Bloom filter
BitMapBloomFilter filter = new BitMapBloomFilter(10);
filter.add("123");
filter.add("abc");
bool exists = filter.contains("abc");</code>Conclusion
Hutool is a cost‑effective utility library that can make Java code more concise and pleasant to write.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.