SpringBoot 项目里常见的 工具类(utils) 其实分三类:
-
Spring 自带的工具类 (
org.springframework.util.*
) -
Apache Commons / Google Guava 等常用工具类(在 SpringBoot 项目里经常配合使用)
-
业务中常见的自定义工具类(时间、JSON、加解密等)
下面整理一个 SpringBoot 常用 utils 工具类和常用方法清单:
一、Spring 自带的工具类(常用在 SpringBoot 中)
这些类大部分在 org.springframework.util
包下。
1. 字符串工具类
-
StringUtils
-
StringUtils.hasText(str)
:是否有非空白字符 -
StringUtils.hasLength(str)
:是否非null
且长度大于 0 -
StringUtils.isEmpty(str)
:是否为null
或空串 -
StringUtils.collectionToDelimitedString(list, ",")
:集合转逗号分隔字符串 -
StringUtils.tokenizeToStringArray(str, ",")
:分割字符串
-
2. 集合工具类
-
CollectionUtils
-
CollectionUtils.isEmpty(list)
:判断集合是否为空 -
CollectionUtils.arrayToList(array)
:数组转 List -
CollectionUtils.mergeArrayIntoCollection(array, collection)
:合并数组到集合
-
-
ObjectUtils
-
ObjectUtils.isEmpty(obj)
:判断对象是否为空(支持数组、集合、Map) -
ObjectUtils.nullSafeEquals(a, b)
:安全比较两个对象
-
3. Assert 断言工具类
-
Assert
-
Assert.notNull(obj, "xxx不能为空")
-
Assert.hasText(str, "字符串不能为空")
-
Assert.isTrue(flag, "条件不满足")
👉 常用于参数校验,避免手写
if (...) throw ...
-
4. 资源 & IO 工具类
-
FileCopyUtils
-
FileCopyUtils.copyToByteArray(inputStream)
:复制流到字节数组 -
FileCopyUtils.copy(inputStream, outputStream)
:流之间复制
-
-
StreamUtils
StreamUtils.copyToString(inputStream, Charset)
:把 InputStream 转成 String
-
ResourceUtils
ResourceUtils.getFile("classpath:config.yml")
:获取资源文件
5. Bean 相关工具类
-
BeanUtils
-
BeanUtils.copyProperties(source, target)
:复制属性(常用 DTO -> Entity) -
BeanUtils.instantiateClass(clazz)
:实例化对象
-
-
ReflectionUtils
-
ReflectionUtils.doWithFields(clazz, fieldCallback)
:遍历类字段 -
ReflectionUtils.makeAccessible(field)
:设置反射可访问
-
6. 环境/配置工具
-
SystemPropertyUtils
SystemPropertyUtils.resolvePlaceholders("${java.home}")
:解析占位符
-
AntPathMatcher
matcher.match("/api/**", "/api/user/1")
:Ant 风格路径匹配
二、第三方常用工具类(在 SpringBoot 中广泛使用)
Apache Commons Lang
-
StringUtils.isBlank(str)
/StringUtils.isNotBlank(str)
-
StringUtils.join(list, ",")
-
RandomStringUtils.randomAlphanumeric(8)
Apache Commons Collections
-
CollectionUtils.isEmpty(coll)
-
CollectionUtils.union(a, b)
Google Guava
-
Lists.newArrayList()
-
Maps.newHashMap()
-
Strings.isNullOrEmpty(str)
三、业务常见自定义工具类
在实际 SpringBoot 项目里,经常会自己封装工具类,例如:
-
DateTimeUtil
-
格式化、解析日期(基于
java.time
) -
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
-
-
JsonUtil(通常封装 Jackson)
-
toJson(obj)
→ String -
fromJson(json, clazz)
→ 对象
-
-
ResponseUtil
- 统一返回结果
Result.success(data)
/Result.fail("错误")
- 统一返回结果
-
EncryptUtil
- MD5、SHA256、AES、RSA 等加解密
-
IdUtil
- 生成 UUID、雪花算法 ID
✅ 总结:
SpringBoot 项目里最常用的工具类主要是 Spring 自带的 StringUtils
、CollectionUtils
、ObjectUtils
、Assert
、BeanUtils
等;
然后结合 Apache Commons / Guava 来增强;最后根据业务写一些 时间/JSON/加解密 工具类。
整理一个 SpringBoot 常用工具类速查表(CheatSheet) ,里面包含 Spring 自带 + 常用三方工具 + 业务自定义常见工具,并给上示例代码。
🚀 SpringBoot 常用 Utils 工具类速查表
1. 字符串工具类
工具类 | 方法 | 示例 |
---|---|---|
org.springframework.util.StringUtils |
hasText(str) |
StringUtils.hasText("abc") → true |
isEmpty(str) |
StringUtils.isEmpty("") → true |
|
collectionToDelimitedString(list, ",") |
→ "a,b,c" |
|
Apache Commons StringUtils |
isBlank(str) |
StringUtils.isBlank(" ") → true |
join(list, ",") |
→ "1,2,3" |
|
Guava Strings |
isNullOrEmpty(str) |
Strings.isNullOrEmpty("") → true |
2. 集合工具类
工具类 | 方法 | 示例 |
---|---|---|
CollectionUtils (Spring) |
isEmpty(list) |
CollectionUtils.isEmpty(new ArrayList<>()) → true |
arrayToList(array) |
→ [1, 2, 3] |
|
Apache Commons CollectionUtils |
union(a, b) |
→ A ∪ B |
Guava Lists |
newArrayList("a","b") |
快速构建 List |
Guava Maps |
newHashMap() |
快速构建 Map |
3. 对象工具类
工具类 | 方法 | 示例 |
---|---|---|
ObjectUtils (Spring) |
isEmpty(obj) |
ObjectUtils.isEmpty(new int[]{}) → true |
nullSafeEquals(a,b) |
ObjectUtils.nullSafeEquals("a","a") → true |
4. Bean 工具类
工具类 | 方法 | 示例 |
---|---|---|
BeanUtils (Spring) |
copyProperties(src, target) |
DTO → Entity 拷贝 |
ReflectionUtils |
doWithFields(clazz, callback) |
遍历字段,常用于注解处理 |
5. 参数校验工具类
工具类 | 方法 | 示例 |
---|---|---|
Assert (Spring) |
notNull(obj, "不能为空") |
如果 obj==null 抛异常 |
hasText(str, "不能为空") |
如果为空字符串抛异常 |
6. 文件 & IO 工具类
工具类 | 方法 | 示例 |
---|---|---|
FileCopyUtils (Spring) |
copyToByteArray(is) |
InputStream → byte[] |
StreamUtils |
copyToString(is, Charset) |
InputStream → String |
ResourceUtils |
getFile("classpath:config.yml") |
获取资源文件 |
7. 路径 & 表达式工具
工具类 | 方法 | 示例 |
---|---|---|
AntPathMatcher |
match("/api/**", "/api/user/1") |
返回 true |
SystemPropertyUtils |
resolvePlaceholders("${java.home}") |
解析系统变量 |
8. 业务常见自定义工具类
工具类 | 常用方法 | 示例 |
---|---|---|
DateTimeUtil |
format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss") |
→ "2025-09-23 15:30:00" |
JsonUtil (封装 Jackson) |
toJson(obj) / fromJson(str, clazz) |
对象 ↔ JSON |
ResponseUtil |
success(data) / fail(msg) |
统一返回结果 |
EncryptUtil |
md5(str) / aesEncrypt(text,key) |
加密解密 |
IdUtil |
uuid() / snowflakeId() |
唯一 ID 生成 |