【Java后端】SpringBoot 常用工具类和工具方法汇总

SpringBoot 项目里常见的 工具类(utils) 其实分三类:

  1. Spring 自带的工具类org.springframework.util.*

  2. Apache Commons / Google Guava 等常用工具类(在 SpringBoot 项目里经常配合使用)

  3. 业务中常见的自定义工具类(时间、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 项目里,经常会自己封装工具类,例如:

  1. DateTimeUtil

    • 格式化、解析日期(基于 java.time

    • LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

  2. JsonUtil(通常封装 Jackson)

    • toJson(obj) → String

    • fromJson(json, clazz) → 对象

  3. ResponseUtil

    • 统一返回结果 Result.success(data) / Result.fail("错误")
  4. EncryptUtil

    • MD5、SHA256、AES、RSA 等加解密
  5. IdUtil

    • 生成 UUID、雪花算法 ID

✅ 总结:

SpringBoot 项目里最常用的工具类主要是 Spring 自带的 StringUtilsCollectionUtilsObjectUtilsAssertBeanUtils 等;

然后结合 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 生成
相关推荐
小蒜学长5 小时前
springboot宠物领养救助平台的开发与设计(代码+数据库+LW)
java·数据库·spring boot·后端·宠物
别看我只是一直狼5 小时前
【保姆级教程】2025年最新版!从零到一搭建 Spring Boot 项目,后端小白也能轻松上手!
java·spring boot
Q_Q5110082856 小时前
python+springboot+uniapp微信小程序“美好食荐”系统 美食推荐 菜谱展示 用户互动 评论收藏系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
Emma歌小白6 小时前
查看阿里云服务器的.jar文件所在的目录
spring boot
java_python源码7 小时前
springboot+vue考研复习小程序(源码+文档+调试+基础修改+答疑)
vue.js·spring boot·小程序
风象南8 小时前
基于SpringBoot + QLExpress打造动态规则引擎
spring boot·后端
西门吹雪@13211 小时前
springboot项目添加请求链路追踪日志traceId
java·spring boot·后端
邂逅星河浪漫12 小时前
【Spring AI】Ollama大模型-智能对话实现+项目实战(Spring Boot + Vue)
java·人工智能·spring boot·vue·prompt·agent·ollama
moxiaoran575312 小时前
Springboot实现WebSocket通信(二)
spring boot·后端·websocket