作者pttnews (PTT新闻)
看板java
标题[问题] 有关PropertyUtils.getSimpleProperty
时间Tue Dec 15 22:37:00 2015
一个很简单的 jObject
class Pojo {
public Pojo(){}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
试图利用apache的beanutils, get value by PropertyName
import org.apache.commons.beanutils.PropertyUtils;
@Test
public void TestGetProperty() {
Pojo testCase_2 = new Pojo();
testCase_2.setName("my name is google");
try {
System.out.println(testCase_2.getName());
String name =
(String) PropertyUtils.getSimpleProperty(testCase_2,"name");
} catch (Exception e) {
e.printStackTrace();
}
}
结果出现Error
java.lang.NoSuchMethodException:
Property 'name' has no getter method in class 'class info.diywork.test.Pojo'
at
org.apache.commons.beanutils.PropertyUtilsBean.
getSimpleProperty(PropertyUtilsBean.java:1327)
at
org.apache.commons.beanutils.PropertyUtils.
getSimpleProperty(PropertyUtils.java:611)
上网G很久,请问要怎麽使用BeanUtitls getVal by name
自问自答:
因为Pojo 不是Public Class
囧~~~~~~~~~~~~~~~~~
※ 编辑: pttnews (49.216.167.103), 12/16/2015 16:57:57