一、消息格式
1 <xml> 2 <ToUserName>< ![CDATA[toUser] ]> 3 </ToUserName> 4 <FromUserName>< ![CDATA[fromUser] ]> 5 </FromUserName> 6 <CreateTime>1348831860</CreateTime> 7 <MsgType>< ![CDATA[text] ]> 8 </MsgType> 9 <Content>< ![CDATA[this is a test] ]> 10 </Content> 11 <MsgId>1234567890123456</MsgId> 12 </xml>
二、参数介绍
参数 | 描述 |
---|---|
ToUserName | 开发者微信号 |
FromUserName | 发送方帐号(一个OpenID) |
CreateTime | 消息创建时间 (整型) |
MsgType | image |
PicUrl | 图片链接(由系统生成) |
MediaId | 图片消息媒体id,可以调用多媒体文件下载接口拉取数据。 |
MsgId | 消息id,64位整型 |
三、文本消息实体类
1 public class TextMessage { 2 private String ToUserName;//开发者微信号 3 private String FromUserName;//发送方帐号(一个OpenID) 4 private String CreateTime;//消息创建时间 (整型) 5 private String MsgType;//消息类型 6 private String Content;//内容 7 private String MsgId;//消息id,64位整型 8 public String getToUserName() { 9 return ToUserName; 10 } 11 public void setToUserName(String toUserName) { 12 ToUserName = toUserName; 13 } 14 public String getFromUserName() { 15 return FromUserName; 16 } 17 public void setFromUserName(String fromUserName) { 18 FromUserName = fromUserName; 19 } 20 public String getCreateTime() { 21 return CreateTime; 22 } 23 public void setCreateTime(String createTime) { 24 CreateTime = createTime; 25 } 26 public String getMsgType() { 27 return MsgType; 28 } 29 public void setMsgType(String msgType) { 30 MsgType = msgType; 31 } 32 public String getContent() { 33 return Content; 34 } 35 public void setContent(String content) { 36 Content = content; 37 } 38 public String getMsgId() { 39 return MsgId; 40 } 41 public void setMsgId(String msgId) { 42 MsgId = msgId; 43 } 44 @Override 45 public String toString() { 46 return "TextMessage [ToUserName=" + ToUserName + ", FromUserName=" 47 + FromUserName + ", CreateTime=" + CreateTime + ", MsgType=" 48 + MsgType + ", Content=" + Content + ", MsgId=" + MsgId + "]"; 49 } 50 51 }
三、编写消息类(读取xml)
1.消息类型及事件类型
1 package com.weixin.msgparam; 2 /** 3 * 常用参数类 4 * @author 付先生 5 * @date 2018年1月26日 下午4:40:50 6 * @TODO TODO 7 */ 8 public class MsgTypeParam { 9 10 public static final String MESSAGE_TEXT="text";//文本 11 public static final String MESSAGE_IMAGE="image";//图片 12 public static final String MESSAGE_NEWS="news"; 13 public static final String MESSAGE_VOICE="voice";//语音 14 public static final String MESSAGE_VIDEO="video";//视频 15 public static final String MESSAGE_MUSIC="music";//音乐 16 public static final String MESSAGE_LOCATION="location";//位置 17 public static final String MESSAGE_LINK="link";//链接消息 18 public static final String MESSAGE_EVENT="event";//事件 19 public static final String MESSAGE_SUBSCRIBE="subscribe";//关注 20 public static final String MESSAGE_UNSUBSCRIBE="unsubscribe";//取消关注 21 public static final String MESSAGE_CLICK="CLICK";//点击 22 public static final String MESSAGE_VIEW="VIEW";//点击菜单跳转链接时的事件推送 23 public static final String MESSAGE_SCANCODE= "scancode_push";//扫码 24 }
2.MessageUtil.java --》解析接收的xml信息、封装返回的xml信息
注意:这里用到了2个JAR包:xstream-1.3.jar、dom4j.jar
public class MessageUtil { /** * 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 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 * 注意事项:添加xstream.jar */ public static String textMessageToXml(TextMessage textMessage){ //XStream xStream = new XStream(new StaxDriver()); xStream.alias("xml", textMessage.getClass()); return xStream.toXML(textMessage); } /** * xStream本身不支持生成cdata块生成,对xstream扩展,让其自动生成cdata块 */ private static XStream xStream = new XStream(new StaxDriver(){ public HierarchicalStreamWriter createWriter(Writer out){ return new PrettyPrintWriter(out){ boolean cdata = true; public void startNode(String name,Class clazz){ super.startNode(name, clazz); } protected void writeText(QuickWriter writer,String text){ if(cdata){ writer.write("<![CDATA["); writer.write(text); writer.write("]]>"); }else{ writer.write(text); } } }; } }); }
3.初始化文本信息方法
/** * 初始化消息内容工具类 * @author 付先生 * @date 2018年1月26日 下午4:49:25 * @TODO TODO */ public class InitMsgContentUtil { //初始化文本消息 public static String initText(String toUserName,String fromUserName,String content){ String message = ""; TextMessage text = new TextMessage(); text.setFromUserName(toUserName); text.setToUserName(fromUserName); text.setMsgType(MsgTypeParam.MESSAGE_TEXT); text.setCreateTime(new Date().getTime()+""); text.setContent(content); message = MessageUtil.textMessageToXml(text); return message; } }
四、修改Servlet中的Post方法,加入如下代码:
1 @Override 2 protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { 3 req.setCharacterEncoding("UTF-8"); 4 resp.setCharacterEncoding("UTF-8"); 5 PrintWriter out = resp.getWriter(); 6 try { 7 Map<String, String> map = MessageUtil.xmlToMap(req); 8 String fromUserName = map.get("FromUserName"); 9 String toUserName = map.get("ToUserName"); 10 //消息类型 11 String msgType = map.get("MsgType"); 12 String content = map.get("Content"); 13 //回复文本消息 14 String message = null; 15 //消息类型判断 16 if(MsgTypeParam.MESSAGE_TEXT.equals(msgType)){ 17 String text = "山东京帝软件微信公众号测试"; 18 //调用初始化文本消息方法 19 message = InitMsgContentUtil.initText(toUserName, fromUserName, text); 20 } 21 out.print(message); 22 } catch (DocumentException e) { 23 e.printStackTrace(); 24 }finally{ 25 out.close(); 26 } 27 }
五、项目部署及查看结果
向公众号发送信息,看是否能够正常返回信息。
给各位推荐一个在线测试的平台:http://debug.fangbei.org/
操作步骤:
录入我们接入微信平台的URL和TOKEN
选择消息类型:文本
内容:输入要发送给公众号的内容,在右侧可看到效果的预览。