在spring boot项目中,开发者通常会依赖一些工具类和api来简化开发、提高效率。以下是一些常用的工具类及其典型应用场景,涵盖 spring 原生工具、第三方库(如hutool、guava) 和 java 自带工具。
1. spring framework 自带工具类
(1) stringutils
- 包名:
org.springframework.util.stringutils
- 功能: 字符串判空、分割、拼接等。
- 常用方法:
boolean isempty(object str); // 判断字符串是否为空(比java原生更安全) string[] tokenizetostringarray(...); // 字符串分割 string collectiontodelimitedstring(...); // 集合转字符串(如用逗号连接)
(2) collectionutils
- 包名:
org.springframework.util.collectionutils
- 功能: 集合操作。
boolean isempty(collection<?> coll); // 判断集合是否为空 boolean containsany(collection<?> source, collection<?> candidates); // 检查是否有交集
(3) filecopyutils
- 包名:
org.springframework.util.filecopyutils
- 功能: 文件复制、流操作。
byte[] copytobytearray(file file); // 文件转字节数组 void copy(inputstream in, outputstream out); // 流复制
(4) resourceutils
- 包名:
org.springframework.util.resourceutils
- 功能: 资源文件读取。
file getfile(string location); // 获取资源文件(如classpath:config.yml)
2. spring boot 特有工具
(1) objectmapper (json处理)
- 包名:
com.fasterxml.jackson.databind.objectmapper
- 场景: json序列化/反序列化(spring boot默认集成jackson)。
string json = objectmapper.writevalueasstring(obj); // 对象转json user user = objectmapper.readvalue(json, user.class); // json转对象
(2) resttemplate / webclient (http请求)
- 包名:
org.springframework.web.client.resttemplate
(同步) org.springframework.web.reactive.function.client.webclient
(异步)- 示例:
string result = resttemplate.getforobject("https://api.example.com", string.class);
(3) jdbctemplate (数据库操作)
- 包名:
org.springframework.jdbc.core.jdbctemplate
- 场景: 简化jdbc操作。
list<user> users = jdbctemplate.query("select * from user", new beanpropertyrowmapper<>(user.class));
3. 第三方工具库
(1) apache commons stringutils
boolean isblank = org.apache.commons.lang3.stringutils.isblank(str); // 判断空白字符串
fileutils
:
fileutils.copyfile(srcfile, destfile); // 文件复制
(2) google guava 集合工具:
list<string> list = lists.newarraylist("a", "b"); // 快速创建集合
字符串处理:
string joined = joiner.on(",").join(list); // 集合拼接为字符串
(3) hutool(国产神器)
strutil
:
boolean isempty = strutil.isempty(str); // 字符串判空
dateutil
:
string now = dateutil.now(); // 当前时间(格式:yyyy-mm-dd hh:mm:ss)
idutil
:
string uuid = idutil.randomuuid(); // 生成uuid
4. java 原生工具类
(1) collections 集合操作:
collections.sort(list); // 排序 collections.reverse(list); // 反转
(2)arrays 数组操作:
list<string> list = arrays.aslist("a", "b"); // 数组转list
(3) files & paths (nio)
文件操作:
byte[] bytes = files.readallbytes(paths.get("file.txt")); // 读取文件
5. 其他高频工具
(1) validationutils (参数校验)
包名: org.springframework.validation.validationutils
示例:
validationutils.rejectifempty(errors, "name", "field.required"); // 校验字段非空
(2) reflectionutils (反射工具)
- 包名:
org.springframework.util.reflectionutils
- 场景: 动态调用方法、访问字段。
reflectionutils.findmethod(user.class, "getname"); // 查找方法
(3) stopwatch (性能监控)
包名: org.springframework.util.stopwatch
示例:
stopwatch watch = new stopwatch(); watch.start("task1"); // 执行代码... watch.stop(); system.out.println(watch.prettyprint()); // 打印耗时
总结:如何选择工具类?
场景 | 推荐工具类 |
---|---|
字符串操作 | stringutils (spring/commons/hutool) |
集合处理 | collectionutils (spring/guava) |
json转换 | objectmapper (jackson) |
文件读写 | fileutils (commons) / files (nio) |
http请求 | resttemplate / webclient |
数据库操作 | jdbctemplate |
日期处理 | dateutil (hutool) |
反射调用 | reflectionutils (spring) |
合理使用这些工具类可以减少重复代码,提升开发效率。如果是spring boot项目,优先使用spring生态提供的工具类(如stringutils
),复杂场景再引入第三方库(如hutool)。
到此这篇关于springboot项目中常用的工具类和api的文章就介绍到这了,更多相关springboot工具类和api内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论