前言
微信的消息响应是很常用的,来看一下。
当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。
接收:
回复:
这里就涉及到xml转为map,文本消息转为xml
实现
实体类
package cn.notemi.po;
/**
* Title:TextMessage
* Description:
*
* @author Flicker
* @create 2017-07-25 下午 8:40
**/
public class TextMessage {
private String ToUserName;
private String FromUserName;
private String CreateTime;
private String MsgType;
private String Content;
private String MsgId;
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;
}
public String getMsgId() {
return MsgId;
}
public void setMsgId(String msgId) {
MsgId = msgId;
}
}
消息转换的工具类
package cn.notemi.util;
import cn.notemi.po.TextMessage;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.servlet.http.HttpServletRequest;
import java.awt.font.TextMeasurer;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Title:MessageUtil
* Description:消息的转换
*
* @author Flicker
* @create 2017-07-25 下午 8:19
**/
public class MessageUtil {
/**
* 将xml转为Map对象
* @param request
* @return
* @throws Exception
*/
public static Map<String,String> xmlToMap(HttpServletRequest request) throws Exception{
Map<String,String> map = new HashMap<String, String>();
SAXReader reader = new SAXReader();
InputStream ins = request.getInputStream();
Document doc = reader.read(ins);
Element root = doc.getRootElement();
List<Element> list = root.elements();
for (Element e:list){
map.put(e.getName(),e.getText());
}
ins.close();
return map;
}
/**
* 将文本消息对象转为xml
* @param textMessage
* @return
*/
public static String textMessageToXml(TextMessage textMessage){
XStream xStream = new XStream();
xStream.alias("xml",type);
return xStream.toXML(textMessage);
}
}
maven引入
这里使用了XStream和SAXReader
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>
Controller
微信消息响应用到的是Post提交。
package cn.notemi.controller;
import cn.notemi.po.TextMessage;
import cn.notemi.util.CheckUtil;
import cn.notemi.util.MessageUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
/**
* Title:WeixinController
* Description:
*
* @author Flicker
* @create 2017-07-25 下午 6:15
**/
@Controller
@RequestMapping(value = "/weixin")
public class WeixinController {
private static Logger logger = Logger.getLogger(WeixinController.class);
@RequestMapping(value = "/connect",method = RequestMethod.GET)
public void connect(HttpServletResponse response, HttpServletRequest request) throws Exception{
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
PrintWriter out = response.getWriter();
if (CheckUtil.checkSignature(signature,timestamp,nonce)){
out.print(echostr);
logger.info("写入返回值成功");
}
}
@RequestMapping(value = "/connect",method = RequestMethod.POST)
public void message(HttpServletRequest request,
HttpServletResponse response)throws Exception{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
Map<String,String> map = MessageUtil.xmlToMap(request);
String toUserName = map.get("ToUserName");
String fromUserName = map.get("FromUserName");
String createTime = map.get("CreateTime");
String msgType = map.get("MsgType");
String content = map.get("Content");
String msgId = map.get("MsgId");
String message = null;
if ("text".equals(msgType)){
TextMessage text = new TextMessage();
text.setFromUserName(toUserName);
text.setToUserName(fromUserName);
text.setMsgType("text");
text.setCreateTime(String.valueOf(new Date().getTime()));
text.setContent("您发送的消息是:"+content);
message = MessageUtil.textMessageToXml(text);
logger.info(message);
}
out.print(message);
out.close();
}
}
测试
Comments (暂无评论)