我目前正在使用 here 中的示例将我的 ResultSet
映射到自定义 Object
。我已经测试了 BeanUtils.setProperty() ,看起来该对象需要 setter 才能工作。有什么方法可以像 Gson 那样映射值吗?我不想有公共(public)设置者。
最佳答案
不,不能。
顾名思义,BeanUtils 处理 Java Beans
They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods.
(强调我的。)
虽然BeanUtils方法的Javadoc相当模糊,但也可以在BeanUtilsBean.setProperty()
的源代码中找到线索。和 PropertyUtilsBean.setSimpleProperty()
.
请参阅documentation也是:
The Java language provides classes like java.beans.Introspector, which can examine a Java class at runtime and identify for you the names of the property getter and setter methods, [...]. The APIs in the BeanUtils package are intended to simplify getting and setting bean properties dynamically, [...].
(强调我的。)
关于java - BeanUtils.setProperty() 可以在没有 setter 的情况下设置值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45641433/