使用下邊的方法,或者轉成fastjson的JsonObject對象
http://blog.csdn.net/cuidiwhere/article/details/8130434
1. 為什么要實現javaBean與Map<String,Object>相互轉換?
用過spring的都知道spring的MVC框架中有一個BaseCommandController對象,利用這個對象我們就可以很方便的將從 客戶端傳遞過來的參數封裝到一個JavaBean對象中去,而不需要我們 request.getParameter("name");bean.setName(name);了,從而也簡化了不少的工作。如果大家用過 BeanUtils.populate的話,就知道,這個方法是可以很方便的將request提交的頁面表單自動填寫到你創建的對象中
2. 如何實現javaBean與Map<String,Object>相互轉換?
方法1: 利用java.beans.Introspector和java.beans.PropertyDescriptor實現 javaBean與Map<String,Object>互轉
方法2: 利用org.apache.commons.beanutils.BeanUtils工具類,BeanUtils.populate實現Map 轉換為javaBean
- package javaStudyDemo.bean.reflect.test;
-
- import java.beans.BeanInfo;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Method;
- import java.util.HashMap;
- import java.util.Map;
- import javaStudyDemo.others.PersonBean;
-
- import org.apache.commons.beanutils.BeanUtils;
-
- /**
- * 當把Person類作為BeanUtilTest的內部類時,程序出錯<br>
- * java.lang.NoSuchMethodException: Property '**' has no setter method<br>
- * 本質:內部類 和 單獨文件中的類的區別 <br>
- * BeanUtils.populate方法的限制:<br>
- * The class must be public, and provide a public constructor that accepts no arguments. <br>
- * This allows tools and applications to dynamically create new instances of your bean, <br>
- * without necessarily knowing what Java class name will be used ahead of time
- */
- public class BeanUtilTest {
-
- public static void main(String[] args) {
-
- PersonBean person = new PersonBean();
- Map<String, Object> mp = new HashMap<String, Object>();
- mp.put("name", "Mike");
- mp.put("age", 25);
- mp.put("mN", "male");
-
- // 將map轉換為bean
- transMap2Bean2(mp, person);
-
- System.out.println("--- transMap2Bean Map Info: ");
- for (Map.Entry<String, Object> entry : mp.entrySet()) {
- System.out.println(entry.getKey() + ": " + entry.getValue());
- }
-
- System.out.println("--- Bean Info: ");
- System.out.println("name: " + person.getName());
- System.out.println("age: " + person.getAge());
- System.out.println("mN: " + person.getmN());
-
- // 將javaBean 轉換為map
- Map<String, Object> map = transBean2Map(person);
-
- System.out.println("--- transBean2Map Map Info: ");
- for (Map.Entry<String, Object> entry : map.entrySet()) {
- System.out.println(entry.getKey() + ": " + entry.getValue());
- }
-
- }
-
- // Map --> Bean 2: 利用org.apache.commons.beanutils 工具類實現 Map --> Bean
- public static void transMap2Bean2(Map<String, Object> map, Object obj) {
- if (map == null || obj == null) {
- return;
- }
- try {
- BeanUtils.populate(obj, map);
- } catch (Exception e) {
- System.out.println("transMap2Bean2 Error " + e);
- }
- }
-
- // Map --> Bean 1: 利用Introspector,PropertyDescriptor實現 Map --> Bean
- public static void transMap2Bean(Map<String, Object> map, Object obj) {
-
- try {
- BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
- PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
-
- for (PropertyDescriptor property : propertyDescriptors) {
- String key = property.getName();
-
- if (map.containsKey(key)) {
- Object value = map.get(key);
- // 得到property對應的setter方法
- Method setter = property.getWriteMethod();
- setter.invoke(obj, value);
- }
-
- }
-
- } catch (Exception e) {
- System.out.println("transMap2Bean Error " + e);
- }
-
- return;
-
- }
-
- // Bean --> Map 1: 利用Introspector和PropertyDescriptor 將Bean --> Map
- public static Map<String, Object> transBean2Map(Object obj) {
-
- if(obj == null){
- return null;
- }
- Map<String, Object> map = new HashMap<String, Object>();
- try {
- BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
- PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
- for (PropertyDescriptor property : propertyDescriptors) {
- String key = property.getName();
-
- // 過濾class屬性
- if (!key.equals("class")) {
- // 得到property對應的getter方法
- Method getter = property.getReadMethod();
- Object value = getter.invoke(obj);
-
- map.put(key, value);
- }
-
- }
- } catch (Exception e) {
- System.out.println("transBean2Map Error " + e);
- }
-
- return map;
-
- }
- }
- public class PersonBean {
-
- private String name;
- private Integer age;
- private String mN;
-
- /**
- * @return the mN
- */
- public String getmN() {
- return mN;
- }
-
- /**
- * @param mN the mN to set
- */
- public void setmN(String mN) {
- this.mN = mN;
- }
-
-
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
-
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
-
- /**
- * @return the age
- */
- public Integer getAge() {
- return age;
- }
-
- /**
- * @param age the age to set
- */
- public void setAge(Integer age) {
- this.age = age;
- }
-
- }
總結: javaBean與Map<String,Object>互轉利用到了java的內省( Introspector )和反射(reflect)機制。 其思路為: 通 過類 Introspector 來獲取某個對象的 BeanInfo 信息,然后通過 BeanInfo 來獲取屬性的描述器 PropertyDescriptor,再利用屬性描述器獲取某個屬性對應的 getter/setter 方法,然后通過反射機制來getter和 setter。
什么是內???
內 省是 Java 語言對 Bean 類屬性、事件的一種缺省處理方法。例如類 PersonBean中有屬性 name, 那我們可以通 過 getName,setName 來得到其值或者設置新的值。通過 getName/setName 來訪問 name 屬性,這就是默認的規 則。 Java 中提供了一套 API 用來訪問某個屬性的 getter/setter 方法,通過這些 API 可以使你不需要了解這個規則(但你最 好還是要搞清楚),這些 API 存放于包 java.beans 中。注意: PersonBean中屬性mN的getter/setter方法必須滿足javaBean命名規范,即getmN,不能寫作getMN,否則轉換失敗。 詳情參考 http://blog.renren.com/share/236384819/5598710664
posted on 2015-06-30 18:44
SIMONE 閱讀(843)
評論(0) 編輯 收藏 所屬分類:
JAVA