接口认证完成后就可以正常开放公众号了,对于用户文本消息处理,根据官方文档消息格式,官方文档,文本消息格式如下:
<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>
对于用户发给公众号的消息如果是:“测试消息”,我们的服务器收到公众平台转发的的xml报文将是(除了接口认证采用的是get方法,其他消息全部采用post方法):
<xml>
<ToUserName><![CDATA[公众号的ID]]></ToUserName>
<FromUserName><![CDATA[发送消息用户的ID]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[测试消息]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
我们需要对接收到的报文进行xml解析,然后根据Content节点的内容进行相应处理,然后通过HttpServletResponse返回即可。
1.在AuthServlet.java中加入doPost方法:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.debug("接收到消息!");
//设置编码格式
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
Map<String, String> map = null;
try {
//将接收的消息转换成map
map = MessageUtil.xmlToMap(req);
} catch (Exception e) {
e.printStackTrace();
}
String toUserName = map.get("ToUserName");
String fromUserName = map.get("FromUserName");
String msgType = map.get("MsgType");
String content = map.get("Content");
String rtnMsg = null;
//判断消息类型
if("text".equals(msgType)) { //文本消息
TextMessage textMsg = new TextMessage();
textMsg.setToUserName(fromUserName);//从消息发送方在返回时变成消息接收方
textMsg.setFromUserName(toUserName);
textMsg.setMsgType(msgType);
textMsg.setCreateTime(new Date().getTime());
textMsg.setContent("您发送的消息是:" + content);
//返回消息
rtnMsg = MessageUtil.textMessageToXml(textMsg);
log.debug("返回的消息:" + rtnMsg);
resp.getWriter().print(rtnMsg);
}
2.新建一个TextMessage.java,这是个普通的java类:
public class TextMessage {
/** 开发者微信号 **/
private String ToUserName;
/** 发送方帐号(一个OpenID) **/
private String FromUserName;
/** 消息创建时间 (整型) **/
private long CreateTime;
/** text **/
private String MsgType;
/** 文本消息内容 **/
private String Content;
/** 消息id,64位整型 **/
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 long getCreateTime() {
return CreateTime;
}
public void setCreateTime(long 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;
}
}
新建一个消息处理类MessageUtil.java:
public class MessageUtil {
private static final Logger log = Logger.getLogger(MessageUtil.class);
/**
* 将xml消息转换成map
*
* @param req
* HttpServletRequest对象
* @return 返回一个map
* @throws Exception
*/
public static Map<String, String> xmlToMap(HttpServletRequest req) throws Exception {
log.debug("进入xmlToMap()");
SAXReader reader = new SAXReader();
Map<String, String> map = new HashMap<String, String>();
InputStream is = is = req.getInputStream();
Document doc = doc = reader.read(is);
// 获取根元素
Element root = doc.getRootElement();
List<Element> list = root.elements();
// 遍历元素并放到map里
for (Element e : list) {
map.put(e.getName(), e.getText());
}
is.close();
log.debug("接收消息转换成map:" + map);
return map;
}
public static String textMessageToXml(TextMessage textMsg) {
XStream xstream = new XStream();
// 将根元素替换成<xml>,默认根元素为<类名>
xstream.alias("xml", textMsg.getClass());
return xstream.toXML(textMsg);
}
}
然后将其部署到公网服务器上即可。
这里比较麻烦的是没法调试,下节(java微信公众号开发总结(3)——调试方法总结(包括本地调试))总结一下一些调试的方法。