微信官方文档:
一、接收普通消息(这里以文本消息举例)
当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。
文本消息(text)
<xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName>
<FromUserName>< ![CDATA[fromUser] ]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType>< ![CDATA[text] ]></MsgType>
<Content>< ![CDATA[this is a test] ]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
参数 | 描述 |
---|---|
ToUserName | 开发者微信号 |
FromUserName | 发送方帐号(一个OpenID) |
CreateTime | 消息创建时间 (整型) |
MsgType | text |
Content | 文本消息内容 |
MsgId | 消息id,64位整型 |
二、回复文本消息
<xml>
<ToUserName>< ![CDATA[toUser] ]></ToUserName>
<FromUserName>< ![CDATA[fromUser] ]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType>< ![CDATA[text] ]></MsgType>
<Content>< ![CDATA[你好] ]></Content>
</xml>
参数 | 是否必须 | 描述 |
---|---|---|
ToUserName | 是 | 接收方帐号(收到的OpenID) |
FromUserName | 是 | 开发者微信号 |
CreateTime | 是 | 消息创建时间 (整型) |
MsgType | 是 | text |
Content | 是 | 回复的消息内容(换行:在content中能够换行,微信客户端就支持换行显示) |
=================================================================================================================
下面附上我的代码
一、
TestMsgEntity.class实体类
package com.xingshang.entity; /** * 这里一定要注意, * ToUserName,FromUserName,CreateTime,MsgType,Content这些参数的大小写 * 一定要与微信开发文档中参数一模一样才行,最好直接复制过来,要不会报错 * */ public class TestMsgEntity { private String ToUserName; private String FromUserName; private String CreateTime; private String MsgType; private String Content; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public String getCreateTime() { return CreateTime; } public void setCreateTime(String createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public String getContent() { return Content; } public void setContent(String content) { Content = content; } }
二、MsgAgeUtil.class工具类
package com.xingshang.util; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.thoughtworks.xstream.XStream; import com.xingshang.entity.TestMsgEntity; public class MsgAgeUtil { /** * 把XML转化为map * * @param request * @return * @throws IOException * @throws DocumentException */ public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException { Map<String, String> map = new HashMap<String, String>(); SAXReader saxReader = new SAXReader(); // 获取用户输入的文件流 ServletInputStream inputStream = request.getInputStream(); // 将文件流转换成map Document doc = saxReader.read(inputStream); // 得到根节点 Element root = doc.getRootElement(); // 得到子节点 List<Element> elements = root.elements(); for (Element element : elements) { map.put(element.getName(), element.getText()); } return map; } /** * 将实体类转换成xml * * @param testMsgEntity * @return */ public static String testMsgToXml(TestMsgEntity testMsgEntity) { XStream xStream = new XStream(); xStream.alias("xml", testMsgEntity.getClass());// 将完整的class路径替换成xml return xStream.toXML(testMsgEntity); } }
三、
package com.xingshang.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.xingshang.entity.TestMsgEntity; import com.xingshang.util.CheCkeUtil; import com.xingshang.util.MsgAgeUtil; @WebServlet("/WeiXinServlet") public class WeiXinServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public WeiXinServlet() { super(); // TODO Auto-generated constructor stub } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 第一步:接受四个参数 String signature = request.getParameter("signature");// 微信加密签名 String timestamp = request.getParameter("timestamp");// 时间戳 String nonce = request.getParameter("nonce");// 随机数 String echostr = request.getParameter("echostr");// 随机字符串 PrintWriter printWriter = response.getWriter(); // 第二部:判断,如果为true,返回字符串echostr if (CheCkeUtil.cheCkeSignature(signature, timestamp, nonce)) { printWriter.write(echostr); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); // 接收用户发送消息 PrintWriter writer = response.getWriter();// 往客户端发送一个文件流 try { // 1、用户发送內容 微信会推送xml消息 将XML转换成map Map<String, String> xmlMap = MsgAgeUtil.xmlToMap(request); System.out.println("xmlMap:" + xmlMap); String msgType = xmlMap.get("MsgType"); String result = null; if (msgType.equals("text")) { TestMsgEntity testMsgEntity = new TestMsgEntity(); testMsgEntity.setToUserName(xmlMap.get("FromUserName")); testMsgEntity.setFromUserName(xmlMap.get("ToUserName")); testMsgEntity.setCreateTime(System.currentTimeMillis() + ""); testMsgEntity.setMsgType("text"); String userContent = xmlMap.get("Content"); String content = null; if (userContent.equals("淄博天气")) { content = "淄博天气,今天最低温度17度,最高温度30度,晴"; } else if (userContent.equals("你好")) { content = "谢谢您的关系"; } else { content = "您的消息我们已经收到,我们会及时处理的!"; } testMsgEntity.setContent(content); // 2、将返回的消息转换为xml result = MsgAgeUtil.testMsgToXml(testMsgEntity); } System.out.println("result:" + result); // 往客户端发送反回消息 writer.write(result); } catch (Exception e) { System.out.println(e.getMessage()); } finally { // 最后把文件流关闭 writer.close(); } } }
最后运行程序(这里一定要注意,一定要保持连接外网状态),成功自动回复文本消息