在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)。