Backend Development 7 min read

Introducing Hutool: A Comprehensive Java Utility Library and Its Core Tools

This article introduces the Hutool Java utility library, outlines its core modules such as SecureUtil, HtmlUtil, and CronUtil, provides code examples for encryption, HTML escaping, and task scheduling, and encourages developers to explore its comprehensive set of backend development tools.

Java Captain
Java Captain
Java Captain
Introducing Hutool: A Comprehensive Java Utility Library and Its Core Tools

Hutool (a blend of "Hu" and "tool") is a Java utility library designed to simplify code, reduce boilerplate, and make Java development more pleasant. It originated from the author’s personal "util" package and has grown to include many non‑business‑related functions.

The library wraps common JDK functionalities for files, streams, encryption, encoding, regex, threading, XML, and more, providing a wide range of utility classes, including:

hutool-aop – JDK dynamic proxy wrapper offering aspect support without IoC.

hutool-bloomFilter – Bloom filter implementation with various hash algorithms.

hutool-cache – Caching utilities.

hutool-core – Core utilities such as Bean operations, date handling, and general helpers.

hutool-cron – Scheduling module supporting Crontab‑style expressions.

hutool-crypto – Encryption and decryption utilities.

hutool-db – JDBC wrapper based on the ActiveRecord pattern.

hutool-dfa – Multi‑keyword search based on DFA models.

hutool-extra – Extensions for third‑party integrations (template engines, mail, etc.).

hutool-http – Http client built on HttpUrlConnection.

hutool-log – Automatic detection of logging implementations.

hutool-script – Script execution wrapper (e.g., JavaScript).

hutool-setting – Advanced configuration file and Properties handling.

hutool-system – System parameter utilities (JVM info, etc.).

hutool-json – JSON handling.

hutool-captcha – Image captcha generation.

In a recent project (Halo), the author replaced several pieces of code with Hutool utilities, finding the experience very smooth. The following commonly used tools are highlighted:

SecureUtil (Encryption/Decryption)

SecureUtil is used for password hashing during login and password changes. Since passwords are stored as MD5 hashes, the login process simply hashes the input before querying the database.

user = userService.userLoginByName(loginName, SecureUtil.md5(loginPwd));

HtmlUtil (HTML Utilities)

HtmlUtil.encode is frequently used to escape characters, preventing XSS and SQL injection. For example, when saving a comment author:

comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor()));

This converts potentially dangerous tags like <script> into safe escaped forms, ensuring the script does not execute.

Other useful HtmlUtil methods include:

HtmlUtil.restoreEscaped – Restores escaped HTML characters.

HtmlUtil.cleanHtmlTag – Removes all HTML tags.

HtmlUtil.removeHtmlTag – Removes specified tags and their content.

HtmlUtil.unwrapHtmlTag – Strips specified tags but keeps inner content.

HtmlUtil.removeHtmlAttr – Removes attributes from HTML tags.

HtmlUtil.removeAllHtmlAttr – Removes all attributes from specified tags.

HtmlUtil.filter – Filters HTML text to prevent XSS attacks.

CronUtil (Task Scheduling)

CronUtil enables scheduling without external frameworks like Quartz. By adding a simple configuration file under resources and starting the scheduler at application startup, tasks such as daily backups can be defined.

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 * * ?

In the application’s event listener, the scheduler is started as follows:

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    this.loadActiveTheme();
    this.loadOptions();
    this.loadFiles();
    this.loadThemes();
    // Start scheduled tasks
    CronUtil.start();
    log.info("Scheduled tasks started successfully!");
}

These three utilities—SecureUtil, HtmlUtil, and CronUtil—demonstrate how Hutool can streamline common backend tasks. Developers are encouraged to explore the other modules, as Hutool offers a very complete set of tools for Java development.

Official website: http://www.hutool.cn/
JavaBackend DevelopmentHutoolUtility LibraryCronUtilHtmlUtilSecureUtil
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.