json数据赋值到bean方法中后,怎么动态调用getter方法。有需求按顺序导出,所以不能死板的定getter顺序。
反射可以,已经解决了。大体思路是先用那个bean来new一个新的对象,然后通过method方法和你组装的getter方法的字符串(比如getSex,注意不需要括号)来获得method。然后通过调用invoke方法,将已经赋值的那个bean带入就可以了。
不知道反射能不能满足你的需求
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
private int value;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
System.out.println("call getValue...");
return this.value;
}
public static void main(String[] args) throws InterruptedException {
Test t = new Test();
t.setValue(123);
try {
for(Method method:t.getClass().getDeclaredMethods()){
if(method.getName().startsWith("get")){
System.out.println(method.invoke(t));
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}