[問題] 有關PropertyUtils.getSimpleProperty

看板java作者 (PTT新聞)時間9年前 (2015/12/15 22:37), 9年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
一個很簡單的 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
文章代碼(AID): #1MS2MFLw (java)
文章代碼(AID): #1MS2MFLw (java)