Boost Your Java Projects with Hutool: Essential Utilities and Real‑World Examples
This article introduces Hutool, a comprehensive Java utility library, outlines its main modules such as SecureUtil, HtmlUtil, and CronUtil, provides code snippets for common tasks like password hashing, HTML escaping, and scheduling, and shares practical tips for integrating these tools into applications.
What is Hutool?
Hutool is an open‑source Java utility library that wraps common JDK functionality—file handling, streams, encryption, encoding, regular expressions, threading, XML, etc.—into concise, easy‑to‑use classes.
Main Modules
hutool-aop – JDK dynamic‑proxy based AOP support without an IoC container.
hutool-bloomFilter – Bloom filter implementation with several hash algorithms.
hutool-cache – Simple caching utilities.
hutool-core – Core utilities such as Bean operations, date handling, and a collection of generic Util classes.
hutool-cron – Cron‑style scheduling without external dependencies.
hutool-crypto – Encryption and decryption helpers.
hutool-db – JDBC wrapper following the ActiveRecord pattern.
hutool-dfa – Multi‑keyword search based on DFA.
hutool-extra – Extensions for third‑party integrations (template engines, mail, etc.).
hutool-http – HttpClient built on HttpURLConnection.
hutool-log – Automatic detection of the underlying logging implementation.
hutool-script – Script execution utilities (e.g., JavaScript).
hutool-setting – Enhanced configuration file and properties handling.
hutool-system – System‑level information such as JVM details.
hutool-json – JSON processing utilities.
hutool-captcha – Image‑based CAPTCHA generation.
Practical Examples
SecureUtil – Password hashing
When authenticating a user whose password is stored as an MD5 hash, the following one‑liner encrypts the input and queries the database:
user = userService.userLoginByName(loginName, SecureUtil.md5(loginPwd));HtmlUtil – Preventing XSS and SQL injection
Encode user‑generated content before persisting it to avoid script execution attacks:
comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor()));Additional HtmlUtil methods include restoreEscaped, cleanHtmlTag, removeHtmlTag, unwrapHtmlTag, removeHtmlAttr, removeAllHtmlAttr, and filter for comprehensive HTML sanitisation.
CronUtil – Lightweight scheduling
CronUtil enables cron‑style tasks without a framework like Quartz. Define tasks in a cron.setting file and start the scheduler at application startup:
cc.ryanc.halo.web.controller.admin.BackupController.backupResources = 0 0 1 * * ?
cc.ryanc.halo.web.controller.admin.BackupController.backupDatabase = 0 0 1 * * ?
cc.ryanc.halo.web.controller.admin.BackupController.backupPosts = 0 0 1 * * ?
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
// other initialization code
CronUtil.start();
log.info("定时任务启动成功!");
}Full configuration documentation: http://hutool.mydoc.io/?t=255673
Getting Started
Add Hutool to a Maven or Gradle project and use it immediately to reduce boilerplate code across Java applications. Official site: http://www.hutool.cn/
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.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.
