【Java】Map转换器

描述: 在控制层接收参数时候, 往往会出现Json格式需要转换为Bean.

           通常一两个字段可以用new去save pojo, 但字段多的情况呢?

           以下就是为了解决这个尴尬情况,  自己写一个转换工具类吧!

 

package com.lwc.demo;

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;

/**
 * @author eddie.lee
 * @Package com.lwc.demo
 * @ClassName Test09
 * @description map converter
 * @date created in 2018-03-22 14:53
 * @modified by
 */

public class Test09 {

	/**
	 * Map converter bean
	 * 
	 * @param clazz
	 * @param map
	 * @param <T>
	 * @return
	 */
	public static <T> T toBean(Class<T> clazz, Map map) {
		T obj = null;
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
			obj = clazz.newInstance();

			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (int i = 0; i < propertyDescriptors.length; i++) {
				PropertyDescriptor descriptor = propertyDescriptors[i];
				String propertyName = descriptor.getName();
				if (map.containsKey(propertyName)) {
					Object value = map.get(propertyName);
					if ("".equals(value)) {
						value = null;
					}
					Object[] args = new Object[1];
					args[0] = value;
					try {
						descriptor.getWriteMethod().invoke(obj, args);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return (T) obj;
	}

	/**
	 * bean converter map
	 * 
	 * @param bean
	 * @return
	 */
	public static Map toMap(Object bean) {
		Class<? extends Object> clazz = bean.getClass();
		Map<Object, Object> returnMap = new HashMap<>(16);
		BeanInfo beanInfo = null;
		try {
			beanInfo = Introspector.getBeanInfo(clazz);
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			for (int i = 0; i < propertyDescriptors.length; i++) {
				PropertyDescriptor descriptor = propertyDescriptors[i];
				String propertyName = descriptor.getName();
				if (!propertyName.equals("class")) {
					Method readMethod = descriptor.getReadMethod();
					Object result = null;
					result = readMethod.invoke(bean, new Object[0]);
					if (null != propertyName) {
						propertyName = propertyName.toString();
					}
					if (null != result) {
						result = result.toString();
					}
					returnMap.put(propertyName, result);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnMap;
	}

	public void MapConverterBean() {
		Map map = new HashMap<>(16);
		map.put("id", "1");
		map.put("name", "eddie");
		map.put("password", "123456");
		TestPojo testPojo = toBean(TestPojo.class, map);
		System.out.println("\nMapConverterBean => " + testPojo + "\n");
	}

	public void BeanConverterMap() {
		TestPojo pojo = new TestPojo();
		pojo.setId("2");
		pojo.setName("jenny");
		pojo.setPassword("123456");
		Map map = toMap(pojo);
		System.out.println("BeanConverterMap => " + map + "\n");
	}

	public static void main(String[] args) {
		Test09 t = new Test09();
		t.MapConverterBean();
		t.BeanConverterMap();
	}

	static class TestPojo {

		private String id;

		private String name;

		private String password;

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public TestPojo(String id, String name, String password) {
			this.id = id;
			this.name = name;
			this.password = password;
		}

		@Override
		public String toString() {
			return "TestPojo{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", password='" + password + '\'' + '}';
		}

		public TestPojo() {
		}
	}
}

 

result:

MapConverterBean => TestPojo{id='1', name='eddie', password='123456'}

BeanConverterMap => {password=123456, name=jenny, id=2}

posted @ 2018-03-22 17:39  Eddie.Lee  阅读(334)  评论(0编辑  收藏  举报