springboot项目中常用的工具类和api

在Spring Boot项目中,开发者通常会依赖一些工具类API 来简化开发、提高效率。以下是一些常用的工具类及其典型应用场景,涵盖 Spring 原生工具第三方库(如Hutool、Guava)Java 自带工具


1. Spring Framework 自带工具类

(1) StringUtils

  • 包名 : org.springframework.util.StringUtils

  • 功能: 字符串判空、分割、拼接等。

  • 常用方法 :

    java 复制代码
    boolean isEmpty(Object str);          // 判断字符串是否为空(比Java原生更安全)
    String[] tokenizeToStringArray(...);  // 字符串分割
    String collectionToDelimitedString(...); // 集合转字符串(如用逗号连接)

(2) CollectionUtils

  • 包名 : org.springframework.util.CollectionUtils

  • 功能 : 集合操作。

    java 复制代码
    boolean isEmpty(Collection<?> coll);  // 判断集合是否为空
    boolean containsAny(Collection<?> source, Collection<?> candidates); // 检查是否有交集

(3) FileCopyUtils

  • 包名 : org.springframework.util.FileCopyUtils

  • 功能 : 文件复制、流操作。

    java 复制代码
    byte[] copyToByteArray(File file);    // 文件转字节数组
    void copy(InputStream in, OutputStream out); // 流复制

(4) ResourceUtils

  • 包名 : org.springframework.util.ResourceUtils

  • 功能 : 资源文件读取。

    java 复制代码
    File getFile(String location);        // 获取资源文件(如classpath:config.yml)

2. Spring Boot 特有工具

(1) ObjectMapper (JSON处理)

  • 包名 : com.fasterxml.jackson.databind.ObjectMapper

  • 场景 : JSON序列化/反序列化(Spring Boot默认集成Jackson)。

    java 复制代码
    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(异步)

  • 示例 :

    java 复制代码
    String result = restTemplate.getForObject("https://api.example.com", String.class);

(3) JdbcTemplate (数据库操作)

  • 包名 : org.springframework.jdbc.core.JdbcTemplate

  • 场景 : 简化JDBC操作。

    java 复制代码
    List<User> users = jdbcTemplate.query("SELECT * FROM user", new BeanPropertyRowMapper<>(User.class));

3. 第三方工具库

(1) Apache Commons

  • StringUtils :

    java 复制代码
    boolean isBlank = org.apache.commons.lang3.StringUtils.isBlank(str); // 判断空白字符串
  • FileUtils :

    java 复制代码
    FileUtils.copyFile(srcFile, destFile); // 文件复制

(2) Google Guava

  • 集合工具 :

    java 复制代码
    List<String> list = Lists.newArrayList("a", "b"); // 快速创建集合
  • 字符串处理 :

    java 复制代码
    String joined = Joiner.on(",").join(list); // 集合拼接为字符串

(3) Hutool(国产神器)

  • StrUtil :

    java 复制代码
    boolean isEmpty = StrUtil.isEmpty(str); // 字符串判空
  • DateUtil :

    java 复制代码
    String now = DateUtil.now(); // 当前时间(格式:yyyy-MM-dd HH:mm:ss)
  • IdUtil :

    java 复制代码
    String uuid = IdUtil.randomUUID(); // 生成UUID

4. Java 原生工具类

(1) Collections

  • 集合操作 :

    java 复制代码
    Collections.sort(list);               // 排序
    Collections.reverse(list);            // 反转

(2) Arrays

  • 数组操作 :

    java 复制代码
    List<String> list = Arrays.asList("a", "b"); // 数组转List

(3) Files & Paths (NIO)

  • 文件操作 :

    java 复制代码
    byte[] bytes = Files.readAllBytes(Paths.get("file.txt")); // 读取文件

5. 其他高频工具

(1) ValidationUtils (参数校验)

  • 包名 : org.springframework.validation.ValidationUtils

  • 示例 :

    java 复制代码
    ValidationUtils.rejectIfEmpty(errors, "name", "field.required"); // 校验字段非空

(2) ReflectionUtils (反射工具)

  • 包名 : org.springframework.util.ReflectionUtils

  • 场景 : 动态调用方法、访问字段。

    java 复制代码
    ReflectionUtils.findMethod(User.class, "getName"); // 查找方法

(3) StopWatch (性能监控)

  • 包名 : org.springframework.util.StopWatch

  • 示例 :

    java 复制代码
    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)。

相关推荐
码农BookSea16 小时前
ReAct:让大模型学会边想边做
后端·ai编程
码农BookSea16 小时前
10分钟掌握 JSON-RPC 协议,面试加分、设计不踩坑
后端
daad77717 小时前
wifi_note
运维·服务器·数据库
凤年徐17 小时前
C++手撕红黑树:从0到200行,拿下STL map底层核心
c++·后端·算法
IT_陈寒17 小时前
Python的列表推导式里藏了个坑,差点让我加班到凌晨
前端·人工智能·后端
xixingzhe217 小时前
Mysql统计空间增量
数据库·mysql
递归尽头是星辰17 小时前
Spring Boot 配置排除失效深度解析:时序与机制核心
spring boot·自动配置·bean 加载·exclude失效·组件扫描
程序员萌萌18 小时前
Redis的缓存机制和淘汰策略详解
数据库·redis·缓存机制·淘汰策略
卷无止境18 小时前
podman与docker的区别和生产环境最佳实践
后端
程途知微18 小时前
ConcurrentHashMap线程安全实现原理全解析
java·后端