commons-beanutils可以从bean中检索嵌套值:
public class Person {
Address address;
public Address getAddress() { return address; }
}
public class Address {
String city;
public String getCity() { return city; }
}
var p = new Person();
p.address = new Address();
p.address.city = "Malgudi";
BeanUtils.getNestedProperty(p, "address.city"); // ✅ Malgudi
当数据类型是记录时,如何以类似的紧凑方式实现这一点?BeanUtils
显然不支持记录:
public record Person(Address address) {}
public record Address(String city) {}
var p = new Person(new Address("Malgudi"));
BeanUtils.getNestedProperty(p, "address.city"); // ❌ Unknown property 'address'
1条答案
按热度按时间jaxagkaj1#
虽然我找不到一个可以在一行程序中实现这一点的通用库,但有一种方法可以在jackson-databind的帮助下配置
BeanUtils
来实现这一点: