微信请求格式、及后台响应代码

文本消息回复
微信请求格式:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453

文本消息格式:
<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位整型

Controller:
/**
* 文本消息回复
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "wx_check",method = {RequestMethod.POST})
public void wxCheck2(HttpServletRequest request, HttpServletResponse response)throws Exception{
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter printWriter = response.getWriter();
try {
Map<String, String> map = MessageUtil.xmlToMap(request);
String toUserName = map.get("ToUserName");
String fromUserName = map.get("FromUserName");
String msgType = map.get("MsgType");
String content = map.get("Content");

String message = null;
if (MessageUtil.MESSAGE_TEXT.equals(msgType)) {

 

/**
* 文本消息回复
*/
TextMessage text = new TextMessage();
text.setFromUserName(toUserName);
text.setToUserName(fromUserName);
text.setCreateTime(new Date().getTime()+"");
text.setMsgType("text");
text.setContent("您发送的消息是:"+ content);
message = MessageUtil.textMessageToxml(text);
}
System.out.print(message);
printWriter.print(message);
}catch (DocumentException e){
e.printStackTrace();
}finally {
printWriter.close();
}
}
MessageUtil:
/**
* xml转换为map集合
*
* @param request
* @return
* @throws Exception
*/
public static Map<String, String> xmlToMap(HttpServletRequest request) throws Exception {
Map<String, String> map = new HashMap<>();
SAXReader reader = new SAXReader();

InputStream ins = request.getInputStream();
Document doc = reader.read(ins);
Element root = doc.getRootElement();

List<Element> list = root.elements();
for (Element element : list) {
map.put(element.getName(), element.getText());
}
ins.close();
return map;
}

文本消息实体类:
package com.jl.controller.po;

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;
}
}

MessageUtil:
/**
* 将文本对象转换为xml
*
* @param textMessage
* @return
*/
public static String textMessageToxml(TextMessage textMessage) {
XStream xStream = new XStream();
xStream.alias("xml", textMessage.getClass(http://www.amjmh.com/v/BIBRGZ_558768/));
return xStream.toXML(textMessage);
}

————————————————

微信请求格式、及后台响应代码

上一篇:微信小程序的页面跳转==编程式导航传参 和 标签的方法传参


下一篇:小程序云开发基础