Introduction to the Hutool Java Utility Library and Sample Usage
This article introduces the Hutool Java utility library, describes its main modules such as encryption, HTML handling, and cron scheduling, and provides practical code examples demonstrating how to use SecureUtil, HtmlUtil, and CronUtil to simplify backend development tasks.
Hutool is a Java utility library that aggregates a wide range of helper classes for common tasks such as file handling, streams, encryption/decryption, encoding, regular expressions, threading, and XML processing, aiming to make Java code more concise and pleasant to write.
The library is organized into many modules, including hutool-aop for JDK dynamic proxy AOP, hutool-bloomFilter for Bloom filter implementations, hutool-cache for caching, hutool-core for core utilities like bean operations and date handling, hutool-cron for cron‑style scheduling, hutool-crypto for cryptographic functions, hutool-db for JDBC‑based data access, and many others such as hutool-html , hutool-http , hutool-log , hutool-script , hutool-setting , hutool-system , hutool-json , and hutool-captcha .
SecureUtil (Encryption/Decryption) – In login and password‑change scenarios where passwords are stored as MD5 hashes, Hutool allows a simple call to SecureUtil.md5() :
user = userService.userLoginByName(loginName, SecureUtil.md5(loginPwd));HtmlUtil (HTML Sanitization) – To prevent XSS and SQL injection, the HtmlUtil.encode method converts unsafe characters to safe HTML entities. Example for sanitizing comment author input:
comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor()));The utility also provides methods such as HtmlUtil.restoreEscaped , HtmlUtil.cleanHtmlTag , HtmlUtil.removeHtmlTag , HtmlUtil.unwrapHtmlTag , HtmlUtil.removeHtmlAttr , HtmlUtil.removeAllHtmlAttr , and HtmlUtil.filter for comprehensive HTML processing.
CronUtil (Task Scheduling) – Hutool’s cron scheduler works without external frameworks like Quartz. By defining tasks in a cron.setting file and invoking CronUtil.start() at application startup, scheduled jobs such as daily backups can be executed. Example configuration:
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 * * ?Typical startup code that loads resources and starts the scheduler:
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
this.loadActiveTheme();
this.loadOptions();
this.loadFiles();
this.loadThemes();
// Start scheduled tasks
CronUtil.start();
log.info("定时任务启动成功!");
}The article concludes that Hutool offers a comprehensive set of utilities that greatly simplify backend development in Java, and encourages readers to explore the remaining modules.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
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.