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:
@@ -8,18 +8,11 @@
|
||||
*/
|
||||
package me.chanjar.weixin.cp.util.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
|
||||
@@ -2,11 +2,12 @@ package me.chanjar.weixin.cp.util.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.util.json.WxMediaUploadResultAdapter;
|
||||
import me.chanjar.weixin.common.util.json.WxErrorAdapter;
|
||||
import me.chanjar.weixin.cp.bean.*;
|
||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||
import me.chanjar.weixin.cp.bean.WxCpTag;
|
||||
import me.chanjar.weixin.cp.bean.WxCpUser;
|
||||
|
||||
public class WxCpGsonBuilder {
|
||||
|
||||
|
||||
@@ -8,12 +8,12 @@
|
||||
*/
|
||||
package me.chanjar.weixin.cp.util.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.cp.bean.WxCpUser;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package me.chanjar.weixin.cp.util.xml;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
import me.chanjar.weixin.cp.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(WxCpXmlMessage.class, config_WxCpXmlMessage());
|
||||
map.put(WxCpXmlOutNewsMessage.class, config_WxCpXmlOutNewsMessage());
|
||||
map.put(WxCpXmlOutTextMessage.class, config_WxCpXmlOutTextMessage());
|
||||
map.put(WxCpXmlOutImageMessage.class, config_WxCpXmlOutImageMessage());
|
||||
map.put(WxCpXmlOutVideoMessage.class, config_WxCpXmlOutVideoMessage());
|
||||
map.put(WxCpXmlOutVoiceMessage.class, config_WxCpXmlOutVoiceMessage());
|
||||
return map;
|
||||
}
|
||||
|
||||
private static XStream config_WxCpXmlMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxCpXmlMessage.class);
|
||||
xstream.processAnnotations(WxCpXmlMessage.ScanCodeInfo.class);
|
||||
xstream.processAnnotations(WxCpXmlMessage.SendPicsInfo.class);
|
||||
xstream.processAnnotations(WxCpXmlMessage.SendPicsInfo.Item.class);
|
||||
xstream.processAnnotations(WxCpXmlMessage.SendLocationInfo.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxCpXmlOutImageMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxCpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxCpXmlOutImageMessage.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxCpXmlOutNewsMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxCpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxCpXmlOutNewsMessage.class);
|
||||
xstream.processAnnotations(WxCpXmlOutNewsMessage.Item.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxCpXmlOutTextMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxCpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxCpXmlOutTextMessage.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxCpXmlOutVideoMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxCpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxCpXmlOutVideoMessage.class);
|
||||
xstream.processAnnotations(WxCpXmlOutVideoMessage.Video.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
private static XStream config_WxCpXmlOutVoiceMessage() {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.processAnnotations(WxCpXmlOutMessage.class);
|
||||
xstream.processAnnotations(WxCpXmlOutVoiceMessage.class);
|
||||
return xstream;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package me.chanjar.weixin.cp.util.xml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
import javax.xml.bind.*;
|
||||
|
||||
import me.chanjar.weixin.cp.bean.*;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
|
||||
|
||||
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(
|
||||
WxCpXmlOutMessage.class,
|
||||
WxCpXmlOutImageMessage.class,
|
||||
WxCpXmlOutNewsMessage.class,
|
||||
WxCpXmlOutTextMessage.class,
|
||||
WxCpXmlOutVideoMessage.class,
|
||||
WxCpXmlOutVoiceMessage.class,
|
||||
WxCpXmlMessage.class);
|
||||
} catch (JAXBException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user