Backend Development 6 min read

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.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Introduction to the Hutool Java Utility Library and Sample Usage

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.

JavaBackend DevelopmentHutoolencryptionUtility LibraryCron SchedulingHTML Sanitization
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.