Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

      • 简介
      • Bean工具-BeanUtil
        • 📚介绍
        • 🍭方法
          • Bean和Map转换
          • 1. BeanUtil.beanToMap bean转map
          • 2. BeanUtil.toBean Bean或Map转Bean
          • Bean检查
          • 复制属性
          • 字段值编辑
          • Bean数据填充
          • BeanPath(Bean路径表达式)
          • 常用方法
          • 1. BeanUtil.toBean bean转map
          • 2. BeanUtil.toBean map转bean
          • 3. BeanUtil.beanToMap
          • 4. BeanUtil.copyToList
          • 5. BeanUtil.copyProperties
          • 更多方法
      • DynaBean
      • BeanPath
      • Bean描述-BeanDesc
      • RecordUtil
    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • JavaBean(bean)
Hutool
2026-01-03
目录

Bean工具-BeanUtil

# 📚介绍

Hutool针对JavaBean操作提供了非常便捷的封装,其中包括了Bean转Map、Map转Bean、复制属性等实用功能。

# 🍭方法

首先定义一个测试用的JavaBean:

@Data
public class Person {
    private String name;
    private int age;
}

# Bean和Map转换

# 1. BeanUtil.beanToMap bean转map

此方法会按照JavaBean的getter方法获取属性值,并转换成map。规则为字段名作为key,getter方法返回的值作为value。

Person person = new Person();
person.setName("测试A11");
person.setAge(24);

// {name=测试A11, age=24}
Map<String, Object> map = BeanUtil.beanToMap(person);

如果只是需要部分属性,可以使用BeanUtil.beanToMap(Object bean, String... keys)方法。

// {name=测试A11}
Map<String, Object> map = BeanUtil.beanToMap(person, "name");

如果想自定义是否转换为下划线模式或忽略null值,可以使用BeanUtil.beanToMap(Object bean, boolean isToUnderlineCase,boolean ignoreNullValue)方法。

Person person = new Person();
person.setName(null);
person.setAge(24);

// {age=24}
Map<String, Object> map = BeanUtil.beanToMap(person, false, true);

注意 如果Bean中有静态字段,则在转换时会被忽略。 如果Bean中的字段未提供getter方法,则在转换时会被忽略。 如果Bean中字段与Getter不匹配,则在转换时会被忽略。

# 2. BeanUtil.toBean Bean或Map转Bean

# Bean检查

# 复制属性

# 字段值编辑

# Bean数据填充

# BeanPath(Bean路径表达式)

# 常用方法

  • toBean 对象或Map转Bean
  • beanToMap 将bean的部分属性转换成map
  • copyProperties 复制Bean对象属性
  • copyToList 复制集合中的Bean属性

# 1. BeanUtil.toBean bean转map

 public void toBeanTest() {
     final SubPerson person = new SubPerson();
     person.setAge(14);
     person.setOpenid("11213232");
     person.setName("测试A11");
     person.setSubName("sub名字");

     final Map<?, ?> map = BeanUtil.toBean(person, Map.class);
     Assertions.assertEquals("测试A11", map.get("name"));
     Assertions.assertEquals(14, map.get("age"));
     Assertions.assertEquals("11213232", map.get("openid"));
     // static属性应被忽略
     Assertions.assertFalse(map.containsKey("SUBNAME"));
 }

# 2. BeanUtil.toBean map转bean

public void mapToBeanIgnoreCaseTest() {
   final HashMap<String, Object> map = MapUtil.newHashMap();
   map.put("Name", "Joe");
   map.put("aGe", 12);

   final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.of().setIgnoreCase(true));
   Assertions.assertEquals("Joe", person.getName());
   Assertions.assertEquals(12, person.getAge());
}

# 3. BeanUtil.beanToMap

public void beanToMapTest() {
   final SubPerson person = new SubPerson();
   person.setAge(14);
   person.setOpenid("11213232");
   person.setName("测试A11");
   person.setSubName("sub名字");

   final Map<String, Object> map = BeanUtil.beanToMap(person);

   Assertions.assertEquals("测试A11", map.get("name"));
   Assertions.assertEquals(14, map.get("age"));
   Assertions.assertEquals("11213232", map.get("openid"));
   // static属性应被忽略
   Assertions.assertFalse(map.containsKey("SUBNAME"));
}

# 4. BeanUtil.copyToList

public void copyListTest() {
   final Student student = new Student();
   student.setName("张三");
   student.setAge(123);
   student.setNo(3158L);

   final Student student2 = new Student();
   student2.setName("李四");
   student2.setAge(125);
   student2.setNo(8848L);

   final List<Student> studentList = ListUtil.view(student, student2);
   final List<Person> people = BeanUtil.copyToList(studentList, Person.class);

   Assertions.assertEquals(studentList.size(), people.size());
   for (int i = 0; i < studentList.size(); i++) {
      Assertions.assertEquals(studentList.get(i).getName(), people.get(i).getName());
      Assertions.assertEquals(studentList.get(i).getAge(), people.get(i).getAge());
   }

}

# 5. BeanUtil.copyProperties

public void copyPropertiesTest() {
  final SubPerson person = new SubPerson();
  person.setAge(14);
  person.setOpenid("11213232");
  person.setName("测试A11");
  person.setSubName("sub名字");

  final SubPerson person1 = BeanUtil.copyProperties(person, SubPerson.class);
  Assertions.assertEquals(14, person1.getAge());
  Assertions.assertEquals("11213232", person1.getOpenid());
  Assertions.assertEquals("测试A11", person1.getName());
  Assertions.assertEquals("sub名字", person1.getSubName());
}

# 更多方法

  • createDynaBean 创建动态Bean
  • findEditor 查找类型转换器
  • getBeanDesc 获取Bean描述信息
  • descForEach 遍历Bean的属性
  • getPropertyDescriptors 获得Bean字段描述数组
  • getPropertyDescriptorMap获得字段名和字段描述Map
  • getPropertyDescriptor 获得Bean类属性描述,大小写敏感
  • getFieldValue 获得字段值,通过反射直接获得字段值,并不调用getXXX方法
  • setFieldValue 设置字段值,通过反射设置字段值,并不调用setXXX方法
  • getProperty 获取Bean中的属性值
  • setProperty 设置Bean中的属性值
  • getBeanDesc 获取Bean描述信息
  • fillBean 填充Bean的核心方法
  • fillBeanWithMap使用Map填充Bean对象
  • isMatchName 给定的Bean的类名是否匹配指定类名字符串
  • edit 编辑Bean的字段,static字段不会处理
  • trimStrFields 把Bean里面的String属性做trim操作。此方法直接对传入的Bean做修改
  • isEmpty 判断Bean是否为空对象,空对象表示本身为null或者所有属性都为null
  • isNotEmpty 判断Bean是否为非空对象,非空对象表示本身不为null或者含有非null属性的对象
  • isReadableBean 判断是否为可读的Bean对象
  • isWritableBean 判断是否为可写Bean对象
  • hasSetter 判断是否有Setter方法
  • hasGetter 判断是否有Getter方法
  • hasPublicField 指定类中是否有public类型字段
  • hasNullField 判断Bean是否包含值为null的属性
  • hasEmptyField 判断Bean是否包含值为null的属性,或当字段为CharSequence时,是否为isEmpty(null或"")
  • checkBean 遍历Bean的字段并断言检查字段
  • getFieldName 获取Getter或Setter方法名对应的字段名称
简介
DynaBean

← 简介 DynaBean→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式