public class MapToBeanUtils { private static final Logger logger = LoggerFactory.getLogger(MapToBeanUtils.class); /** * 将javabean实体类转为SortedMap类型,过滤属性值为null和空的 * 然后返回一个SortedMap类型的值 * * @param obj * @return */ public static SortedMap<String, Object> beanToSortedMap(Object obj) { SortedMap<String, Object> params = new TreeMap<String, Object>(); try { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (!"class".equals(name)) { Object value =propertyUtilsBean.getNestedProperty(obj, name); if(StringUtils.isEmpty(value)){ continue; } params.put(name, value); } } } catch (Exception e) { logger.error("javabean实体类转为SortedMap类型异常!",e); e.printStackTrace(); } return params; } /** * javabean实体类转 Map对象 * @param obj * @return */ public static Map<String, Object> beanToMap(Object obj) { Map<String, Object> params = new HashMap<String, Object>(); try { PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (!"class".equals(name)) { params.put(name, propertyUtilsBean.getNestedProperty(obj, name)); } } } catch (Exception e) { logger.error("java bean 转 map 异常!",e); e.printStackTrace(); } return params; } /** * Map 转 javabean * @param type * @param map * @return */ public static final Object toBean(Class<?> type, Map<String, ? extends Object> map){ try { BeanInfo beanInfo = Introspector.getBeanInfo(type); Object obj = type.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); Object[] args = new Object[1]; args[0] = value; descriptor.getWriteMethod().invoke(obj, args); } } return obj; }catch (Exception e){ logger.error("map 转 java bean异常!",e); e.printStackTrace(); } return null; } public static void main(String[] args) { MapToBeanUtils mapUtils = new MapToBeanUtils(); MapToBeanUtils.Test wxapuor =mapUtils.new Test(); wxapuor.setAppid("123456789"); wxapuor.setMchId("00000000"); wxapuor.setTradeType("APP"); wxapuor.setNonceStr(RandomUtils.getNonceStr(null)); wxapuor.setSign(""); SortedMap map= MapToBeanUtils.beanToSortedMap(wxapuor); Set es = map.entrySet(); Iterator it = es.iterator(); while(it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); String k = (String)entry.getKey(); String v = (String)entry.getValue(); System.out.println("key:"+k+" value:"+v); } XStream xStream = new XStream(); xStream.autodetectAnnotations(true); String xml = xStream.toXML(wxapuor); System.out.println("xml:==="+xml); String sml="<xml>\n" + " <appid><![CDATA[123456789]]></appid>\n" + " <mch_id><![CDATA[00000000]]></mch_id>\n" + " <nonce_str><![CDATA[DF334B223E699294764C2BB7AE40D8DB]]></nonce_str>\n" + " <sign><![CDATA[]]></sign>\n" + " <trade_type><![CDATA[APP]]></trade_type>\n" + "</xml>"; XStream xStream1 = new XStream(); xStream1.processAnnotations(MapToBeanUtils.Test.class); xStream1.alias("xml", MapToBeanUtils.Test.class); MapToBeanUtils.Test wxapuors = (MapToBeanUtils.Test)xStream1.fromXML(sml); System.out.println(wxapuors.getAppid()+"=="+wxapuors.getErrCode()); } @XStreamAlias("xml") public class Test{ /** *返回状态码 */ @XStreamAlias("return_code") private String returnCode; /** * 返回信息:如非空,为错误原因 */ @XStreamAlias("return_msg") private String returnMsg; /** * 应用APPID */ @XStreamAlias("appid") private String appid; /** * 商户号 */ @XStreamAlias("mch_id") private String mchId; /** *设备号 */ @XStreamAlias("device_info") private String deviceInfo; /** * 随机字符串 */ @XStreamAlias("nonce_str") private String nonceStr; /** * 签名 */ @XStreamAlias("sign") private String sign; /** * 业务结果 */ @XStreamAlias("result_code") private String resultCode; /** * 错误代码 */ @XStreamAlias("err_code") private String errCode; /** * 错误代码描述 */ @XStreamAlias("err_code_des") private String errCodeDes; /** * 交易类型 */ @XStreamAlias("trade_type") private String tradeType; /** * 预支付交易会话标识 */ @XStreamAlias("prepay_id") private String prepayId; public String getReturnCode() { return returnCode; } public void setReturnCode(String returnCode) { this.returnCode = returnCode; } public String getReturnMsg() { return returnMsg; } public void setReturnMsg(String returnMsg) { this.returnMsg = returnMsg; } public String getAppid() { return appid; } public void setAppid(String appid) { this.appid = appid; } public String getMchId() { return mchId; } public void setMchId(String mchId) { this.mchId = mchId; } public String getDeviceInfo() { return deviceInfo; } public void setDeviceInfo(String deviceInfo) { this.deviceInfo = deviceInfo; } public String getNonceStr() { return nonceStr; } public void setNonceStr(String nonceStr) { this.nonceStr = nonceStr; } public String getSign() { return sign; } public void setSign(String sign) { this.sign = sign; } public String getResultCode() { return resultCode; } public void setResultCode(String resultCode) { this.resultCode = resultCode; } public String getErrCode() { return errCode; } public void setErrCode(String errCode) { this.errCode = errCode; } public String getErrCodeDes() { return errCodeDes; } public void setErrCodeDes(String errCodeDes) { this.errCodeDes = errCodeDes; } public String getTradeType() { return tradeType; } public void setTradeType(String tradeType) { this.tradeType = tradeType; } public String getPrepayId() { return prepayId; } public void setPrepayId(String prepayId) { this.prepayId = prepayId; } @Override public String toString() { return "WxAppPayUnifiedOrderRes{" + "returnCode='" + returnCode + '\'' + ", returnMsg='" + returnMsg + '\'' + ", appid='" + appid + '\'' + ", mchId='" + mchId + '\'' + ", deviceInfo='" + deviceInfo + '\'' + ", nonceStr='" + nonceStr + '\'' + ", sign='" + sign + '\'' + ", resultCode='" + resultCode + '\'' + ", errCode='" + errCode + '\'' + ", errCodeDes='" + errCodeDes + '\'' + ", tradeType='" + tradeType + '\'' + ", prepayId='" + prepayId + '\'' + '}'; } } }
Map javaBean 互转化
猜你喜欢
转载自wangmengbk.iteye.com/blog/2357724
今日推荐
周排行