TopicTest.java 2.43 KB

package test;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.diligrp.cms.domain.CategoryDTO;
import com.diligrp.cms.sdk.CmsClient;
import com.diligrp.mobsite.getway.domain.common.Constant;
import com.diligrp.mobsite.getway.domain.protocol.payment.PaymentBaseReq;

/**
 * TODO
 * @createTime 2015年7月25日
 * @author zhangshirui
 */

public class TopicTest {

	public static void main(String[] args) {
//		CmsClient cmsClient = new CmsClient();
//		List<CategoryDTO> categorys = cmsClient.getCategoryService().listCategory(Constant.TOPIC_CATEGORY,true);
//		String str = JSON.toJSONString(categorys);
//		System.fund.println(str);
		PaymentBaseReq req = new PaymentBaseReq();
		req.setUserId(1110l);
		req.setFormat("json");
		req.setInputCharset("utf-8");

		try {
			Map<String, String > params1 = TopicTest.convertBean(req);

			params1.put("service", "dilipay.bank.list.get");
			params1.put("partnerId", "80122");
			params1.put("signType", "RSA");
			params1.put("format", "json");
			params1.put("inputCharset", "utf-8");

			params1.put("accountId", "1");
			params1.put("bizType", "1");
			System.out.println(params1.toString());
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

	public static Map convertBean(Object bean)
			throws IntrospectionException, IllegalAccessException, InvocationTargetException {
		Class type = bean.getClass();
		Map returnMap = new HashMap();
		BeanInfo beanInfo = Introspector.getBeanInfo(type);

		PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
		for (int i = 0; i< propertyDescriptors.length; i++) {
			PropertyDescriptor descriptor = propertyDescriptors[i];
			String propertyName = descriptor.getName();
			if (!propertyName.equals("class") && !propertyName.equals("token")) {
				Method readMethod = descriptor.getReadMethod();
				Object result = readMethod.invoke(bean, new Object[0]);
				if (result != null) {
					returnMap.put(propertyName, result.toString());
				} else {
					returnMap.put(propertyName, "");
				}
			}
		}
		return returnMap;
	}
}