1、消息格式java
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>
2、參數介紹微信
參數 | 描述 |
---|---|
ToUserName | 開發者微信號 |
FromUserName | 發送方賬號(一個OpenID) |
CreateTime | 消息建立時間 (整型) |
MsgType | image |
PicUrl | 圖片連接(由系統生成) |
MediaId | 圖片消息媒體id,能夠調用多媒體文件下載接口拉取數據。 |
MsgId | 消息id,64位整型 |
3、文本消息實體類dom
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 }
3、編寫消息類(讀取xml)ide
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.jarui
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.初始化文本信息方法this
/** * 初始化消息內容工具類 * @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; } }
4、修改Servlet中的Post方法,加入以下代碼:spa
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 }
5、項目部署及查看結果 debug
向公衆號發送信息,看是否可以正常返回信息。
給各位推薦一個在線測試的平臺:http://debug.fangbei.org/
操做步驟:
錄入咱們接入微信平臺的URL和TOKEN
選擇消息類型:文本
內容:輸入要發送給公衆號的內容,在右側可看到效果的預覽。