修改 map2xml 方法 传递参数中value为空时的bug
This commit is contained in:
parent
12fdf6065b
commit
94a9995b86
@ -1,142 +1,135 @@
|
||||
package com.foxinmy.weixin4j.model;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONCreator;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* 微信支付账户
|
||||
*
|
||||
* @className WeixinPayAccount
|
||||
* @author jy
|
||||
* @date 2015年6月26日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class WeixinPayAccount extends WeixinAccount {
|
||||
|
||||
private static final long serialVersionUID = -2791256176906048632L;
|
||||
/**
|
||||
* 公众号支付请求中用于加密的密钥 Key,可验证商户唯一身份,PaySignKey 对应于支付场景中的 appKey 值
|
||||
*/
|
||||
private String paySignKey;
|
||||
/**
|
||||
* 财付通商户身份的标识
|
||||
*/
|
||||
private String partnerId;
|
||||
/**
|
||||
* 财付通商户权限密钥Key
|
||||
*/
|
||||
private String partnerKey;
|
||||
/**
|
||||
* 微信支付分配的商户号(商户平台版)
|
||||
*/
|
||||
private String mchId;
|
||||
/**
|
||||
* 微信支付分配的子商户号,受理模式下必填(商户平台版)
|
||||
*/
|
||||
private String subMchId;
|
||||
/**
|
||||
* 微信支付分配的设备号(商户平台版)
|
||||
*/
|
||||
private String deviceInfo;
|
||||
/**
|
||||
* 微信支付版本号(如果无则按照mchId来做判断)
|
||||
*/
|
||||
private int version;
|
||||
|
||||
/**
|
||||
* 商户平台版本(V3)字段
|
||||
*
|
||||
* @param appId
|
||||
* 公众号唯一的身份ID
|
||||
* @param appSecret
|
||||
* 调用接口的凭证
|
||||
* @param paySignKey
|
||||
* 支付密钥字符串(必填)
|
||||
* @param mchId
|
||||
* 微信支付分配的商户号(必填)
|
||||
*/
|
||||
public WeixinPayAccount(String appId, String appSecret, String paySignKey,
|
||||
String mchId) {
|
||||
this(appId, appSecret, paySignKey, mchId, null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付商户信息
|
||||
*
|
||||
* @param appId
|
||||
* 公众号唯一的身份ID
|
||||
* @param appSecret
|
||||
* 调用接口的凭证
|
||||
* @param paySignKey
|
||||
* 支付密钥字符串(必填)
|
||||
* @param mchId
|
||||
* 微信支付分配的商户号(V3版本必填)
|
||||
* @param partnerId
|
||||
* 财付通的商户号(V2版本必填)
|
||||
* @param partnerKey
|
||||
* 财付通商户权限密钥Key(V2版本必填)
|
||||
* @param subMchId
|
||||
* 微信支付分配的子商户号,受理模式下必填(商户平台版)
|
||||
* @param deviceInfo
|
||||
* 微信支付分配的设备号(商户平台版)
|
||||
*/
|
||||
@JSONCreator
|
||||
public WeixinPayAccount(@JSONField(name = "id") String appId,
|
||||
@JSONField(name = "secret") String appSecret,
|
||||
@JSONField(name = "paySignKey") String paySignKey,
|
||||
@JSONField(name = "mchId") String mchId,
|
||||
@JSONField(name = "subMchId") String subMchId,
|
||||
@JSONField(name = "deviceInfo") String deviceInfo,
|
||||
@JSONField(name = "partnerId") String partnerId,
|
||||
@JSONField(name = "partnerKey") String partnerKey) {
|
||||
super(appId, appSecret);
|
||||
this.paySignKey = paySignKey;
|
||||
this.mchId = mchId;
|
||||
this.subMchId = subMchId;
|
||||
this.deviceInfo = deviceInfo;
|
||||
this.partnerId = partnerId;
|
||||
this.partnerKey = partnerKey;
|
||||
}
|
||||
|
||||
public String getPaySignKey() {
|
||||
return paySignKey;
|
||||
}
|
||||
|
||||
public String getPartnerId() {
|
||||
return partnerId;
|
||||
}
|
||||
|
||||
public String getPartnerKey() {
|
||||
return partnerKey;
|
||||
}
|
||||
|
||||
public String getMchId() {
|
||||
return mchId;
|
||||
}
|
||||
|
||||
public String getSubMchId() {
|
||||
return subMchId;
|
||||
}
|
||||
|
||||
public String getDeviceInfo() {
|
||||
return deviceInfo;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
if (version == 0) {
|
||||
return StringUtil.isNotBlank(mchId) ? 3 : 2;
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WeixinPayAccount [" + super.toString() + ", paySignKey="
|
||||
+ paySignKey + ", partnerId=" + partnerId + ", partnerKey="
|
||||
+ partnerKey + ", mchId=" + mchId + ", subMchId=" + subMchId
|
||||
+ ", deviceInfo=" + deviceInfo + ", version=" + getVersion()
|
||||
+ "]";
|
||||
}
|
||||
}
|
||||
package com.foxinmy.weixin4j.model;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONCreator;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* 微信支付账户
|
||||
*
|
||||
* @className WeixinPayAccount
|
||||
* @author jy
|
||||
* @date 2015年6月26日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class WeixinPayAccount extends WeixinAccount {
|
||||
|
||||
private static final long serialVersionUID = -2791256176906048632L;
|
||||
/**
|
||||
* 公众号支付请求中用于加密的密钥 Key,可验证商户唯一身份,PaySignKey 对应于支付场景中的 appKey 值
|
||||
*/
|
||||
private String paySignKey;
|
||||
/**
|
||||
* 财付通商户身份的标识
|
||||
*/
|
||||
private String partnerId;
|
||||
/**
|
||||
* 财付通商户权限密钥Key
|
||||
*/
|
||||
private String partnerKey;
|
||||
/**
|
||||
* 微信支付分配的商户号(商户平台版)
|
||||
*/
|
||||
private String mchId;
|
||||
/**
|
||||
* 微信支付分配的子商户号,受理模式下必填(商户平台版)
|
||||
*/
|
||||
private String subMchId;
|
||||
/**
|
||||
* 微信支付分配的设备号(商户平台版)
|
||||
*/
|
||||
private String deviceInfo;
|
||||
/**
|
||||
* 微信支付版本号(如果无则按照mchId来做判断)
|
||||
*/
|
||||
private int version;
|
||||
|
||||
/**
|
||||
* 商户平台版本(V3)字段
|
||||
*
|
||||
* @param appId
|
||||
* 公众号唯一的身份ID
|
||||
* @param appSecret
|
||||
* 调用接口的凭证
|
||||
* @param paySignKey
|
||||
* 支付密钥字符串(必填)
|
||||
* @param mchId
|
||||
* 微信支付分配的商户号(必填)
|
||||
*/
|
||||
public WeixinPayAccount(String appId, String appSecret, String paySignKey, String mchId) {
|
||||
this(appId, appSecret, paySignKey, mchId, null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付商户信息
|
||||
*
|
||||
* @param appId
|
||||
* 公众号唯一的身份ID(必填)
|
||||
* @param appSecret
|
||||
* 调用接口的凭证(必填)
|
||||
* @param paySignKey
|
||||
* 支付密钥字符串(必填)
|
||||
* @param mchId
|
||||
* 微信支付分配的商户号(V3商户平台版必填)
|
||||
* @param subMchId
|
||||
* 微信支付分配的子商户号,受理模式下必填(V3商户平台版 非必须)
|
||||
* @param deviceInfo
|
||||
* 微信支付分配的设备号(V3商户平台版 非必须)
|
||||
* @param partnerId
|
||||
* 财付通的商户号(V2版本必填)
|
||||
* @param partnerKey
|
||||
* 财付通商户权限密钥Key(V2版本必填)
|
||||
*/
|
||||
@JSONCreator
|
||||
public WeixinPayAccount(@JSONField(name = "id") String appId, @JSONField(name = "secret") String appSecret,
|
||||
@JSONField(name = "paySignKey") String paySignKey, @JSONField(name = "mchId") String mchId,
|
||||
@JSONField(name = "subMchId") String subMchId, @JSONField(name = "deviceInfo") String deviceInfo,
|
||||
@JSONField(name = "partnerId") String partnerId, @JSONField(name = "partnerKey") String partnerKey) {
|
||||
super(appId, appSecret);
|
||||
this.paySignKey = paySignKey;
|
||||
this.mchId = mchId;
|
||||
this.subMchId = subMchId;
|
||||
this.deviceInfo = deviceInfo;
|
||||
this.partnerId = partnerId;
|
||||
this.partnerKey = partnerKey;
|
||||
}
|
||||
|
||||
public String getPaySignKey() {
|
||||
return paySignKey;
|
||||
}
|
||||
|
||||
public String getPartnerId() {
|
||||
return partnerId;
|
||||
}
|
||||
|
||||
public String getPartnerKey() {
|
||||
return partnerKey;
|
||||
}
|
||||
|
||||
public String getMchId() {
|
||||
return mchId;
|
||||
}
|
||||
|
||||
public String getSubMchId() {
|
||||
return subMchId;
|
||||
}
|
||||
|
||||
public String getDeviceInfo() {
|
||||
return deviceInfo;
|
||||
}
|
||||
|
||||
public int getVersion() {
|
||||
if (version == 0) {
|
||||
return StringUtil.isNotBlank(mchId) ? 3 : 2;
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WeixinPayAccount [" + super.toString() + ", paySignKey=" + paySignKey + ", partnerId=" + partnerId
|
||||
+ ", partnerKey=" + partnerKey + ", mchId=" + mchId + ", subMchId=" + subMchId + ", deviceInfo="
|
||||
+ deviceInfo + ", version=" + getVersion() + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,280 +1,281 @@
|
||||
package com.foxinmy.weixin4j.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* XML 处理
|
||||
*
|
||||
* @className XmlStream
|
||||
* @author jy
|
||||
* @date 2015年6月2日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public final class XmlStream {
|
||||
private final static String ROOT_ELEMENT_XML = "xml";
|
||||
private final static String XML_VERSION = "1.0";
|
||||
private final static Map<Class<?>, Unmarshaller> messageUnmarshaller;
|
||||
private final static Map<Class<?>, Marshaller> messageMarshaller;
|
||||
static {
|
||||
messageUnmarshaller = new HashMap<Class<?>, Unmarshaller>();
|
||||
messageMarshaller = new HashMap<Class<?>, Marshaller>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Xml2Bean
|
||||
*
|
||||
* @param content
|
||||
* xml内容
|
||||
* @param clazz
|
||||
* bean类型
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromXML(InputStream content, Class<T> clazz) {
|
||||
Unmarshaller unmarshaller = messageUnmarshaller.get(clazz);
|
||||
if (unmarshaller == null) {
|
||||
try {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
||||
unmarshaller = jaxbContext.createUnmarshaller();
|
||||
messageUnmarshaller.put(clazz, unmarshaller);
|
||||
} catch (JAXBException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
Source source = new StreamSource(content);
|
||||
XmlRootElement rootElement = clazz
|
||||
.getAnnotation(XmlRootElement.class);
|
||||
if (rootElement == null || rootElement.name().equals(
|
||||
XmlRootElement.class.getMethod("name")
|
||||
.getDefaultValue().toString())) {
|
||||
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source,
|
||||
clazz);
|
||||
return jaxbElement.getValue();
|
||||
} else {
|
||||
return (T) unmarshaller.unmarshal(source);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
if (content != null) {
|
||||
try {
|
||||
content.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Xml2Bean
|
||||
*
|
||||
* @param content
|
||||
* xml内容
|
||||
* @param clazz
|
||||
* bean类型
|
||||
* @return
|
||||
*/
|
||||
public static <T> T fromXML(String content, Class<T> clazz) {
|
||||
return fromXML(
|
||||
new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* map2xml
|
||||
*
|
||||
* @param map
|
||||
* value无嵌套的map
|
||||
* @return xml内容
|
||||
*/
|
||||
public static String map2xml(Map<String, String> map) {
|
||||
StringWriter sw = new StringWriter();
|
||||
try {
|
||||
XMLStreamWriter xw = XMLOutputFactory.newInstance()
|
||||
.createXMLStreamWriter(sw);
|
||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||
for (Iterator<Entry<String, String>> it = map.entrySet().iterator(); it
|
||||
.hasNext();) {
|
||||
Entry<String, String> entry = it.next();
|
||||
xw.writeStartElement(entry.getKey());
|
||||
xw.writeCData(entry.getValue());
|
||||
xw.writeEndElement();
|
||||
}
|
||||
xw.writeEndDocument();
|
||||
xw.flush();
|
||||
xw.close();
|
||||
} catch (XMLStreamException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
try {
|
||||
sw.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
return sw.getBuffer().toString();
|
||||
}
|
||||
|
||||
public static String map2xml(JSONObject json) {
|
||||
StringWriter sw = new StringWriter();
|
||||
try {
|
||||
XMLStreamWriter xw = XMLOutputFactory.newInstance()
|
||||
.createXMLStreamWriter(sw);
|
||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||
Set<String> keys = json.keySet();
|
||||
for (String key : keys) {
|
||||
xw.writeStartElement(key);
|
||||
xw.writeCData(json.getString(key));
|
||||
xw.writeEndElement();
|
||||
}
|
||||
xw.writeEndDocument();
|
||||
xw.flush();
|
||||
xw.close();
|
||||
} catch (XMLStreamException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
try {
|
||||
sw.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
return sw.getBuffer().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* xml2map
|
||||
*
|
||||
* @param content
|
||||
* 无嵌套节点的xml内容
|
||||
* @return map对象
|
||||
*/
|
||||
public static Map<String, String> xml2map(String content) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
StringReader sr = new StringReader(content);
|
||||
try {
|
||||
XMLStreamReader xr = XMLInputFactory.newInstance()
|
||||
.createXMLStreamReader(sr);
|
||||
while (true) {
|
||||
int event = xr.next();
|
||||
if (event == XMLStreamConstants.END_DOCUMENT) {
|
||||
xr.close();
|
||||
break;
|
||||
} else if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
String name = xr.getLocalName();
|
||||
while (true) {
|
||||
event = xr.next();
|
||||
if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
name = xr.getLocalName();
|
||||
} else if (event == XMLStreamConstants.END_ELEMENT) {
|
||||
break;
|
||||
} else if (event == XMLStreamConstants.CHARACTERS) {
|
||||
String value = xr.getText();
|
||||
map.put(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (XMLStreamException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
sr.close();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bean2Xml
|
||||
*
|
||||
* @param object
|
||||
* bean对象
|
||||
* @return xml内容
|
||||
*/
|
||||
public static String toXML(Object object) {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
toXML(object, os);
|
||||
return StringUtil.newStringUtf8(os.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Bean2Xml
|
||||
*
|
||||
* @param t
|
||||
* bean对象
|
||||
* @param os
|
||||
* 输出流
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> void toXML(T t, OutputStream os) {
|
||||
Class<T> clazz = (Class<T>) t.getClass();
|
||||
Marshaller marshaller = messageMarshaller.get(clazz);
|
||||
if (marshaller == null) {
|
||||
try {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
||||
marshaller = jaxbContext.createMarshaller();
|
||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, Consts.UTF_8.name());
|
||||
messageMarshaller.put(clazz, marshaller);
|
||||
} catch (JAXBException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
XmlRootElement rootElement = clazz
|
||||
.getAnnotation(XmlRootElement.class);
|
||||
if (rootElement == null
|
||||
|| rootElement.name().equals(
|
||||
XmlRootElement.class.getMethod("name")
|
||||
.getDefaultValue().toString())) {
|
||||
marshaller.marshal(new JAXBElement<T>(new QName(
|
||||
ROOT_ELEMENT_XML), clazz, t), os);
|
||||
} else {
|
||||
marshaller.marshal(t, os);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
if (os != null) {
|
||||
try {
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.foxinmy.weixin4j.xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLOutputFactory;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.stream.XMLStreamWriter;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* XML 处理
|
||||
*
|
||||
* @className XmlStream
|
||||
* @author jy
|
||||
* @date 2015年6月2日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public final class XmlStream {
|
||||
private final static String ROOT_ELEMENT_XML = "xml";
|
||||
private final static String XML_VERSION = "1.0";
|
||||
private final static Map<Class<?>, Unmarshaller> messageUnmarshaller;
|
||||
private final static Map<Class<?>, Marshaller> messageMarshaller;
|
||||
|
||||
static {
|
||||
messageUnmarshaller = new HashMap<Class<?>, Unmarshaller>();
|
||||
messageMarshaller = new HashMap<Class<?>, Marshaller>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Xml2Bean
|
||||
*
|
||||
* @param content
|
||||
* xml内容
|
||||
* @param clazz
|
||||
* bean类型
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromXML(InputStream content, Class<T> clazz) {
|
||||
Unmarshaller unmarshaller = messageUnmarshaller.get(clazz);
|
||||
if (unmarshaller == null) {
|
||||
try {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
||||
unmarshaller = jaxbContext.createUnmarshaller();
|
||||
messageUnmarshaller.put(clazz, unmarshaller);
|
||||
} catch (JAXBException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
Source source = new StreamSource(content);
|
||||
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
|
||||
if (rootElement == null
|
||||
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
|
||||
JAXBElement<T> jaxbElement = unmarshaller.unmarshal(source, clazz);
|
||||
return jaxbElement.getValue();
|
||||
} else {
|
||||
return (T) unmarshaller.unmarshal(source);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
if (content != null) {
|
||||
try {
|
||||
content.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Xml2Bean
|
||||
*
|
||||
* @param content
|
||||
* xml内容
|
||||
* @param clazz
|
||||
* bean类型
|
||||
* @return
|
||||
*/
|
||||
public static <T> T fromXML(String content, Class<T> clazz) {
|
||||
return fromXML(new ByteArrayInputStream(content.getBytes(Consts.UTF_8)), clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* map2xml
|
||||
*
|
||||
* @param map
|
||||
* value无嵌套的map
|
||||
* @return xml内容
|
||||
*/
|
||||
public static String map2xml(Map<String, String> map) {
|
||||
StringWriter sw = new StringWriter();
|
||||
try {
|
||||
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
|
||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||
for (Iterator<Entry<String, String>> it = map.entrySet().iterator(); it.hasNext();) {
|
||||
Entry<String, String> entry = it.next();
|
||||
if (StringUtil.isBlank(entry.getValue())) {
|
||||
continue;
|
||||
}
|
||||
xw.writeStartElement(entry.getKey());
|
||||
xw.writeCData(entry.getValue());
|
||||
xw.writeEndElement();
|
||||
}
|
||||
xw.writeEndDocument();
|
||||
xw.flush();
|
||||
xw.close();
|
||||
} catch (XMLStreamException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
try {
|
||||
sw.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
return sw.getBuffer().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* map2xml
|
||||
*
|
||||
* @param json
|
||||
* value无嵌套的json
|
||||
* @return xml内容
|
||||
*/
|
||||
public static String map2xml(JSONObject json) {
|
||||
StringWriter sw = new StringWriter();
|
||||
try {
|
||||
XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
|
||||
xw.writeStartDocument(Consts.UTF_8.name(), XML_VERSION);
|
||||
xw.writeStartElement(ROOT_ELEMENT_XML);
|
||||
for (Iterator<Entry<String, Object>> it = json.entrySet().iterator(); it.hasNext();) {
|
||||
Entry<String, Object> entry = it.next();
|
||||
if (StringUtil.isBlank(json.getString(entry.getKey()))) {
|
||||
continue;
|
||||
}
|
||||
xw.writeStartElement(entry.getKey());
|
||||
xw.writeCData(json.getString(entry.getKey()));
|
||||
xw.writeEndElement();
|
||||
}
|
||||
xw.writeEndDocument();
|
||||
xw.flush();
|
||||
xw.close();
|
||||
} catch (XMLStreamException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
try {
|
||||
sw.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
return sw.getBuffer().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* xml2map
|
||||
*
|
||||
* @param content
|
||||
* 无嵌套节点的xml内容
|
||||
* @return map对象
|
||||
*/
|
||||
public static Map<String, String> xml2map(String content) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
StringReader sr = new StringReader(content);
|
||||
try {
|
||||
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(sr);
|
||||
while (true) {
|
||||
int event = xr.next();
|
||||
if (event == XMLStreamConstants.END_DOCUMENT) {
|
||||
xr.close();
|
||||
break;
|
||||
} else if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
String name = xr.getLocalName();
|
||||
while (true) {
|
||||
event = xr.next();
|
||||
if (event == XMLStreamConstants.START_ELEMENT) {
|
||||
name = xr.getLocalName();
|
||||
} else if (event == XMLStreamConstants.END_ELEMENT) {
|
||||
break;
|
||||
} else if (event == XMLStreamConstants.CHARACTERS) {
|
||||
String value = xr.getText();
|
||||
map.put(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (XMLStreamException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
sr.close();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bean2Xml
|
||||
*
|
||||
* @param object
|
||||
* bean对象
|
||||
* @return xml内容
|
||||
*/
|
||||
public static String toXML(Object object) {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
toXML(object, os);
|
||||
return StringUtil.newStringUtf8(os.toByteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Bean2Xml
|
||||
*
|
||||
* @param t
|
||||
* bean对象
|
||||
* @param os
|
||||
* 输出流
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> void toXML(T t, OutputStream os) {
|
||||
Class<T> clazz = (Class<T>) t.getClass();
|
||||
Marshaller marshaller = messageMarshaller.get(clazz);
|
||||
if (marshaller == null) {
|
||||
try {
|
||||
JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
|
||||
marshaller = jaxbContext.createMarshaller();
|
||||
marshaller.setProperty(Marshaller.JAXB_ENCODING, Consts.UTF_8.name());
|
||||
messageMarshaller.put(clazz, marshaller);
|
||||
} catch (JAXBException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
try {
|
||||
XmlRootElement rootElement = clazz.getAnnotation(XmlRootElement.class);
|
||||
if (rootElement == null
|
||||
|| rootElement.name().equals(XmlRootElement.class.getMethod("name").getDefaultValue().toString())) {
|
||||
marshaller.marshal(new JAXBElement<T>(new QName(ROOT_ELEMENT_XML), clazz, t), os);
|
||||
} else {
|
||||
marshaller.marshal(t, os);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
} finally {
|
||||
if (os != null) {
|
||||
try {
|
||||
os.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,60 +1,60 @@
|
||||
package com.foxinmy.weixin4j.mp.test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.payment.mch.MPPayment;
|
||||
import com.foxinmy.weixin4j.payment.mch.MPPaymentResult;
|
||||
import com.foxinmy.weixin4j.payment.mch.Redpacket;
|
||||
import com.foxinmy.weixin4j.payment.mch.RedpacketRecord;
|
||||
import com.foxinmy.weixin4j.payment.mch.RedpacketSendResult;
|
||||
import com.foxinmy.weixin4j.type.MPPaymentCheckNameType;
|
||||
|
||||
/**
|
||||
* 现金发放测试
|
||||
*
|
||||
* @className CashTest
|
||||
* @author jy
|
||||
* @date 2015年4月1日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class CashTest extends PayTest {
|
||||
|
||||
@Test
|
||||
public void sendRedpacket() throws WeixinException, IOException {
|
||||
Redpacket redpacket = new Redpacket("HB001", "无忧钱庄", "无忧钱庄",
|
||||
"oyFLst1bqtuTcxK-ojF8hOGtLQao", 1d);
|
||||
redpacket.setActName("红包测试");
|
||||
redpacket.setClientIp("127.0.0.1");
|
||||
redpacket.setMaxValue(1d);
|
||||
redpacket.setMinValue(1d);
|
||||
redpacket.setRemark("快来领取红包吧!");
|
||||
redpacket.setTotalNum(1);
|
||||
redpacket.setWishing("来就送钱");
|
||||
RedpacketSendResult result = PAY3.sendRedpack(new FileInputStream(
|
||||
caFile), redpacket);
|
||||
System.err.println(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryRedpacket() throws WeixinException, IOException {
|
||||
String outTradeNo = "HB001";
|
||||
RedpacketRecord record = PAY3.queryRedpack(new FileInputStream(
|
||||
caFile), outTradeNo);
|
||||
System.err.println(record);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mpPayment() throws WeixinException, IOException {
|
||||
MPPayment payment = new MPPayment("MP001",
|
||||
"oyFLst1bqtuTcxK-ojF8hOGtLQao",
|
||||
MPPaymentCheckNameType.NO_CHECK, "企业付款测试", 0.01d, "127.0.0.1");
|
||||
MPPaymentResult result = PAY3.mpPayment(
|
||||
new FileInputStream(caFile), payment);
|
||||
System.err.println(result);
|
||||
}
|
||||
}
|
||||
package com.foxinmy.weixin4j.mp.test;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.payment.mch.MPPayment;
|
||||
import com.foxinmy.weixin4j.payment.mch.MPPaymentResult;
|
||||
import com.foxinmy.weixin4j.payment.mch.Redpacket;
|
||||
import com.foxinmy.weixin4j.payment.mch.RedpacketRecord;
|
||||
import com.foxinmy.weixin4j.payment.mch.RedpacketSendResult;
|
||||
import com.foxinmy.weixin4j.type.MPPaymentCheckNameType;
|
||||
|
||||
/**
|
||||
* 现金发放测试
|
||||
*
|
||||
* @className CashTest
|
||||
* @author jy
|
||||
* @date 2015年4月1日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class CashTest extends PayTest {
|
||||
|
||||
@Test
|
||||
public void sendRedpacket() throws WeixinException, IOException {
|
||||
Redpacket redpacket = new Redpacket("HB001", "无忧钱庄", "无忧钱庄",
|
||||
"oyFLst1bqtuTcxK-ojF8hOGtLQao", 1d);
|
||||
redpacket.setActName("红包测试");
|
||||
redpacket.setClientIp("127.0.0.1");
|
||||
redpacket.setMaxValue(1d);
|
||||
redpacket.setMinValue(1d);
|
||||
redpacket.setRemark("快来领取红包吧!");
|
||||
redpacket.setTotalNum(1);
|
||||
redpacket.setWishing("来就送钱");
|
||||
RedpacketSendResult result = PAY3.sendRedpack(new FileInputStream(
|
||||
caFile), redpacket);
|
||||
System.err.println(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryRedpacket() throws WeixinException, IOException {
|
||||
String outTradeNo = "HB001";
|
||||
RedpacketRecord record = PAY3.queryRedpack(new FileInputStream(
|
||||
caFile), outTradeNo);
|
||||
System.err.println(record);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mpPayment() throws WeixinException, IOException {
|
||||
MPPayment payment = new MPPayment("MP001",
|
||||
"oyFLst1bqtuTcxK-ojF8hOGtLQao",
|
||||
MPPaymentCheckNameType.NO_CHECK, "企业付款测试", 0.01d, "127.0.0.1");
|
||||
MPPaymentResult result = PAY3.mpPayment(
|
||||
new FileInputStream(caFile), payment);
|
||||
System.err.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user