1
0
mirror of synced 2025-12-22 18:08:12 +08:00

issue #71 替换掉JAXB,避免因OpenJDK造成无法deserialize xml的错误

This commit is contained in:
Daniel Qian
2015-01-19 15:12:14 +08:00
parent eaa77bc7b3
commit f3ec0b1965
73 changed files with 834 additions and 986 deletions

View File

@@ -1,46 +1,35 @@
package me.chanjar.weixin.mp.api;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.google.inject.Binder;
import com.google.inject.Module;
import org.xml.sax.InputSource;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import java.io.InputStream;
public class ApiTestModule implements Module {
@Override
public void configure(Binder binder) {
try {
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxXmlMpInMemoryConfigStorage config = fromXml(WxXmlMpInMemoryConfigStorage.class, is1);
WxMpServiceImpl wxService = new WxMpServiceImpl();
wxService.setWxMpConfigStorage(config);
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxXmlMpInMemoryConfigStorage config = fromXml(WxXmlMpInMemoryConfigStorage.class, is1);
WxMpServiceImpl wxService = new WxMpServiceImpl();
wxService.setWxMpConfigStorage(config);
binder.bind(WxMpServiceImpl.class).toInstance(wxService);
binder.bind(WxMpConfigStorage.class).toInstance(config);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
binder.bind(WxMpServiceImpl.class).toInstance(wxService);
binder.bind(WxMpConfigStorage.class).toInstance(config);
}
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
Unmarshaller um = JAXBContext.newInstance(clazz).createUnmarshaller();
InputSource inputSource = new InputSource(is);
inputSource.setEncoding("utf-8");
T object = (T) um.unmarshal(inputSource);
return object;
public static <T> T fromXml(Class<T> clazz, InputStream is) {
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", clazz);
xstream.processAnnotations(clazz);
return (T) xstream.fromXML(is);
}
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
public static class WxXmlMpInMemoryConfigStorage extends WxMpInMemoryConfigStorage {
@XStreamAlias("xml")
public static class WxXmlMpInMemoryConfigStorage extends WxMpInMemoryConfigStorage {
protected String openId;

View File

@@ -1,14 +1,12 @@
package me.chanjar.weixin.mp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.StringUtils;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
/**
* 基础API测试
* @author chanjarster

View File

@@ -1,14 +1,12 @@
package me.chanjar.weixin.mp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
/***
* 测试发送客服消息
* @author chanjarster

View File

@@ -1,15 +1,13 @@
package me.chanjar.weixin.mp.api;
import java.util.List;
import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.WxMpGroup;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
import java.util.List;
/**
* 测试分组接口

View File

@@ -1,10 +1,9 @@
package me.chanjar.weixin.mp.api;
import java.io.IOException;
import java.io.InputStream;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.WxMpMassGroupMessage;
import me.chanjar.weixin.mp.bean.WxMpMassNews;
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
@@ -16,9 +15,8 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
/**
* 测试群发消息

View File

@@ -1,20 +1,18 @@
package me.chanjar.weixin.mp.api;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 测试多媒体文件上传下载

View File

@@ -1,19 +1,15 @@
package me.chanjar.weixin.mp.api;
import javax.xml.bind.JAXBException;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import com.google.inject.Inject;
import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
/**
* 测试菜单
* @author chanjarster
@@ -42,7 +38,7 @@ public class WxMpMenuAPITest {
}
@DataProvider(name="menu")
public Object[][] getMenu() throws JAXBException {
public Object[][] getMenu() {
WxMenu menu = new WxMenu();
WxMenuButton button1 = new WxMenuButton();
button1.setType(WxConsts.BUTTON_CLICK);

View File

@@ -1,7 +1,5 @@
package me.chanjar.weixin.mp.api;
import java.util.Map;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
@@ -9,6 +7,8 @@ import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Map;
/**
* 测试消息路由器
* @author chanjarster

View File

@@ -1,15 +1,13 @@
package me.chanjar.weixin.mp.api;
import java.io.File;
import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
import java.io.File;
/**
* 测试用户相关的接口

View File

@@ -1,13 +1,11 @@
package me.chanjar.weixin.mp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
/**
* 测试短连接
*

View File

@@ -1,15 +1,13 @@
package me.chanjar.weixin.mp.api;
import com.google.inject.Inject;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.google.inject.Inject;
/**
* 测试用户相关的接口
* @author chanjarster

View File

@@ -1,11 +1,10 @@
package me.chanjar.weixin.mp.bean;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage.WxArticle;
import org.testng.Assert;
import org.testng.annotations.Test;
import me.chanjar.weixin.mp.bean.WxMpCustomMessage.WxArticle;
@Test
public class WxMpCustomMessageTest {

View File

@@ -7,7 +7,7 @@ import org.testng.annotations.Test;
public class WxMpXmlOutImageMessageTest {
public void test() {
WxMpMpXmlOutImageMessage m = new WxMpMpXmlOutImageMessage();
WxMpXmlOutImageMessage m = new WxMpXmlOutImageMessage();
m.setMediaId("ddfefesfsdfef");
m.setCreateTime(1122l);
m.setFromUserName("from");
@@ -25,7 +25,7 @@ public class WxMpXmlOutImageMessageTest {
}
public void testBuild() {
WxMpMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId("ddfefesfsdfef").fromUser("from").toUser("to").build();
WxMpXmlOutImageMessage m = WxMpXmlOutMessage.IMAGE().mediaId("ddfefesfsdfef").fromUser("from").toUser("to").build();
String expected = "<xml>"
+ "<ToUserName><![CDATA[to]]></ToUserName>"
+ "<FromUserName><![CDATA[from]]></FromUserName>"

View File

@@ -1,21 +1,16 @@
package me.chanjar.weixin.mp.demo;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import org.xml.sax.InputSource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.InputStream;
/**
* @author Daniel Qian
*/
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@XStreamAlias("xml")
class WxMpDemoInMemoryConfigStorage extends WxMpInMemoryConfigStorage {
@Override
@@ -25,11 +20,10 @@ class WxMpDemoInMemoryConfigStorage extends WxMpInMemoryConfigStorage {
}
public static WxMpDemoInMemoryConfigStorage fromXml(InputStream is) throws JAXBException {
Unmarshaller um = JAXBContext.newInstance(WxMpDemoInMemoryConfigStorage.class).createUnmarshaller();
InputSource inputSource = new InputSource(is);
inputSource.setEncoding("utf-8");
return (WxMpDemoInMemoryConfigStorage) um.unmarshal(inputSource);
public static WxMpDemoInMemoryConfigStorage fromXml(InputStream is) {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxMpDemoInMemoryConfigStorage.class);
return (WxMpDemoInMemoryConfigStorage) xstream.fromXML(is);
}
}

View File

@@ -4,14 +4,14 @@ import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.WxMpMpXmlOutImageMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutImageMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
@@ -41,7 +41,6 @@ public class WxMpDemoServer {
}
private static void initWeixin() {
try {
InputStream is1 = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxMpDemoInMemoryConfigStorage config = WxMpDemoInMemoryConfigStorage.fromXml(is1);
@@ -67,7 +66,7 @@ public class WxMpDemoServer {
try {
WxMediaUploadResult wxMediaUploadResult = wxMpService
.mediaUpload(WxConsts.MEDIA_IMAGE, WxConsts.FILE_JPG, ClassLoader.getSystemResourceAsStream("mm.jpeg"));
WxMpMpXmlOutImageMessage m
WxMpXmlOutImageMessage m
= WxMpXmlOutMessage
.IMAGE()
.mediaId(wxMediaUploadResult.getMediaId())
@@ -117,8 +116,5 @@ public class WxMpDemoServer {
.end()
;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -1,23 +1,17 @@
package me.chanjar.weixin.mp.demo;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.WxMpMpXmlOutImageMessage;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
/**
* @author Daniel Qian

View File

@@ -1,26 +1,15 @@
package me.chanjar.weixin.mp.demo;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.WxMpMpXmlOutImageMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
public class WxMpOAuth2Servlet extends HttpServlet {