mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
issue #71 替换掉JAXB,避免因OpenJDK造成无法deserialize xml的错误
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package me.chanjar.weixin.mp.util.http;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import me.chanjar.weixin.mp.bean.*;
|
||||
import me.chanjar.weixin.mp.bean.result.*;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
|
||||
|
||||
public class WxMpGsonBuilder {
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package me.chanjar.weixin.mp.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
|
||||
|
||||
|
||||
@@ -10,8 +10,6 @@ package me.chanjar.weixin.mp.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
@@ -8,9 +8,10 @@
|
||||
*/
|
||||
package me.chanjar.weixin.mp.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
|
||||
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package me.chanjar.weixin.mp.util.xml;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
import me.chanjar.weixin.mp.bean.*;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by qianjia on 15/1/19.
|
||||
*/
|
||||
public class XStreamTransformer {
|
||||
|
||||
protected static final Map<Class, XStream> CLASS_2_XSTREAM_INSTANCE = configXStreamInstance();
|
||||
|
||||
/**
|
||||
* xml -> pojo
|
||||
*
|
||||
* @param clazz
|
||||
* @param xml
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromXml(Class<T> clazz, String xml) {
|
||||
T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(xml);
|
||||
return object;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromXml(Class<T> clazz, InputStream is) {
|
||||
T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(is);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* pojo -> xml
|
||||
*
|
||||
* @param clazz
|
||||
* @param object
|
||||
* @return
|
||||
*/
|
||||
public static <T> String toXml(Class<T> clazz, T object) {
|
||||
return CLASS_2_XSTREAM_INSTANCE.get(clazz).toXML(object);
|
||||
}
|
||||
|
||||
private static Map<Class, XStream> configXStreamInstance() {
|
||||
Map<Class, XStream> map = new HashMap<Class, XStream>();
|
||||
map.put(WxMpXmlMessage.class, config_WxMpXmlMessage());
|
||||
map.put(WxMpXmlOutMusicMessage.class, config_WxMpXmlOutMusicMessage());
|
||||
map.put(WxMpXmlOutNewsMessage.class, config_WxMpXmlOutNewsMessage());
|
||||
map.put(WxMpXmlOutTextMessage.class, config_WxMpXmlOutTextMessage());
|
||||
map.put(WxMpXmlOutImageMessage.class, config_WxMpXmlOutImageMessage());
|
||||
map.put(WxMpXmlOutVideoMessage.class, config_WxMpXmlOutVideoMessage());
|
||||
map.put(WxMpXmlOutVoiceMessage.class, config_WxMpXmlOutVoiceMessage());
|
||||
return map;
|
||||
}
|
||||
|
||||
private static XStream config_WxMpXmlMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxMpXmlMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlMessage.ScanCodeInfo.class);
|
||||
xstream.processAnnotations(WxMpXmlMessage.SendPicsInfo.class);
|
||||
xstream.processAnnotations(WxMpXmlMessage.SendPicsInfo.Item.class);
|
||||
xstream.processAnnotations(WxMpXmlMessage.SendLocationInfo.class);
|
||||
|
||||
xstream.aliasField("MsgID", WxMpXmlMessage.class, "msgId");
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxMpXmlOutImageMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxMpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutImageMessage.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxMpXmlOutNewsMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxMpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutNewsMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutNewsMessage.Item.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxMpXmlOutMusicMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxMpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutMusicMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutMusicMessage.Music.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxMpXmlOutTextMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxMpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutTextMessage.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxMpXmlOutVideoMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxMpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutVideoMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutVideoMessage.Video.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxMpXmlOutVoiceMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxMpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxMpXmlOutVoiceMessage.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package me.chanjar.weixin.mp.util.xml;
|
||||
|
||||
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
|
||||
import me.chanjar.weixin.mp.bean.*;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.*;
|
||||
|
||||
public class XmlTransformer {
|
||||
|
||||
protected static final JAXBContext JAXB_CONTEXT = initJAXBContext();
|
||||
|
||||
/**
|
||||
* xml -> pojo
|
||||
*
|
||||
* @param clazz
|
||||
* @param xml
|
||||
* @return
|
||||
* @throws JAXBException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromXml(Class<T> clazz, String xml) throws JAXBException {
|
||||
Unmarshaller um = JAXB_CONTEXT.createUnmarshaller();
|
||||
T object = (T) um.unmarshal(new StringReader(xml));
|
||||
return object;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
|
||||
Unmarshaller um = JAXB_CONTEXT.createUnmarshaller();
|
||||
InputSource inputSource = new InputSource(is);
|
||||
inputSource.setEncoding("utf-8");
|
||||
T object = (T) um.unmarshal(inputSource);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* pojo -> xml
|
||||
*
|
||||
* @param clazz
|
||||
* @param object
|
||||
* @return
|
||||
* @throws JAXBException
|
||||
*/
|
||||
public static <T> String toXml(Class<T> clazz, T object) throws JAXBException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
toXml(clazz, object, stringWriter);
|
||||
return stringWriter.getBuffer().toString();
|
||||
}
|
||||
|
||||
public static <T> void toXml(Class<T> clazz, T object, Writer writer) throws JAXBException {
|
||||
Marshaller m = JAXB_CONTEXT.createMarshaller();
|
||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||
m.setProperty(CharacterEscapeHandler.class.getName(), CHAR_ESCAPE_HANDLER);
|
||||
m.setProperty(Marshaller.JAXB_FRAGMENT, true);
|
||||
m.marshal(object, writer);
|
||||
}
|
||||
|
||||
protected static final CharacterEscapeHandler CHAR_ESCAPE_HANDLER = new CharacterUnescapeHandler();
|
||||
|
||||
protected static class CharacterUnescapeHandler implements CharacterEscapeHandler {
|
||||
public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException {
|
||||
writer.write(ac, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
private static JAXBContext initJAXBContext() {
|
||||
/*
|
||||
* JAXBContext对象是线程安全的,根据官方文档的建议将对象作为全局实例
|
||||
* https://jaxb.java.net/guide/Performance_and_thread_safety.html
|
||||
*/
|
||||
try {
|
||||
return JAXBContext.newInstance(
|
||||
WxMpXmlOutMessage.class,
|
||||
WxMpMpXmlOutImageMessage.class,
|
||||
WxMpXmlOutNewsMessage.class,
|
||||
WxMpXmlOutMusicMessage.class,
|
||||
WxMpXmlOutTextMessage.class,
|
||||
WxMpXmlOutVideoMessage.class,
|
||||
WxMpXmlOutVoiceMessage.class,
|
||||
WxMpXmlMessage.class);
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user