Java開發微信公衆號(三)---微信服務器請求消息,響應消息,事件消息以及工具處理類的封裝

在前面幾篇文章咱們講了微信公衆號環境的配置 和微信公衆號服務的接入,接下來咱們來講一下微信服務器請求消息,響應消息以及事件消息的相關內容,首先咱們來分析一下消息類型和返回xml格式及實體類的封裝。html

(一)封裝請求信息

首先打開微信提供的開發者文檔:http://mp.weixin.qq.com/wiki/home/ ,點擊左側的「消息管理」----「接收普通消息」,在右側咱們能夠看到微信普通消息類型大體有:文本消息、圖片消息、語音消息、視頻消息、小視頻消息、地理位置消息、連接消息;經過查看開發者文檔,咱們能夠發現消息類型的格式爲xml,以文本消息爲例:java

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

 當咱們接收消息的時候,微信將向咱們發送Post請求,並以XML的格式發送與接收數據。那麼此時咱們就須要一個工具類來處理XML格式的文件:MessageType.parseXml()git

  1 package com.webchat.util.weixin;
  2 
  3 import java.io.InputStream;
  4 import java.io.Writer;
  5 import java.util.HashMap;
  6 import java.util.List;
  7 import java.util.Map;
  8 
  9 import javax.servlet.http.HttpServletRequest;
 10 import javax.servlet.http.HttpServletResponse;
 11 
 12 import org.dom4j.Document;
 13 import org.dom4j.Element;
 14 import org.dom4j.io.SAXReader;
 15 
 16 import com.thoughtworks.xstream.XStream;
 17 import com.thoughtworks.xstream.core.util.QuickWriter;
 18 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 19 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
 20 import com.thoughtworks.xstream.io.xml.XppDriver;
 21 import com.webchat.entity.PageData;
 22  
 23 public class MessageType {
 24     /*
 25      * 文本消息
 26      */
 27     public static final String TEXT_MESSAGE = "text";
 28     /*
 29      * 圖片消息
 30      */
 31     public static final String IMAGE_MESSAGE = "image";
 32     /*
 33      * 語音消息
 34      */
 35     public static final String VOICE_MESSAGE = "voice";
 36     /*
 37      * 視頻消息
 38      */
 39     public static final String VIDEO_MESSAGE = "video";
 40     /*
 41      * 小視頻消息消息
 42      */
 43     public static final String SHORTVIDEO_MESSAGE = "shortvideo";
 44     /*
 45      * 地理位置消息
 46      */
 47     public static final String POSOTION_MESSAGE = "location";
 48     /*
 49      * 連接消息
 50      */
 51     public static final String LINK_MESSAGE = "link";
 52     /*
 53      * 音樂消息
 54      */
 55     public static final String MUSIC_MESSAGE = "music";
 56     /*
 57      * 圖文消息
 58      */
 59     public static final String IMAGE_TEXT_MESSAGE = "news";
 60     /*
 61      * 請求消息類型:事件推送
 62      */
 63     public static final String REQ_MESSAGE_TYPE_EVENT = "event";
 64     /*
 65      * 事件類型:subscribe(訂閱)
 66      */
 67     public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
 68     /*
 69      * 事件類型:unsubscribe(取消訂閱)
 70      */
 71     public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
 72     /*
 73      * 事件類型:scan(用戶已關注時的掃描帶參數二維碼)
 74      */
 75     public static final String EVENT_TYPE_SCAN = "scan";
 76     /*
 77      * 事件類型:LOCATION(上報地理位置)
 78      */
 79     public static final String EVENT_TYPE_LOCATION = "location";
 80     /*
 81      * 事件類型:CLICK(自定義菜單)
 82      */
 83     public static final String EVENT_TYPE_CLICK = "click";
 84 
 85     /*
 86      * 響應消息類型:文本
 87      */
 88     public static final String RESP_MESSAGE_TYPE_TEXT = "text";
 89     /*
 90      * 響應消息類型:圖片
 91      */
 92     public static final String RESP_MESSAGE_TYPE_IMAGE = "image";
 93     /*
 94      * 響應消息類型:語音
 95      */
 96     public static final String RESP_MESSAGE_TYPE_VOICE = "voice";
 97     /*
 98      * 響應消息類型:視頻
 99      */
100     public static final String RESP_MESSAGE_TYPE_VIDEO = "video";
101     /*
102      * 響應消息類型:音樂
103      */
104     public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
105     /*
106      * 響應消息類型:圖文
107      */
108     public static final String RESP_MESSAGE_TYPE_NEWS = "news";
109 
110     /**
111      * @Title parseXml
112      * @Description 將用戶的xml消息提取成map key value 類型
113      * @param request
114      * @param response
115      * @return
116      * @throws Exception
117      */
118     public static Map<String, String> parseXml(HttpServletRequest request, HttpServletResponse response)
119             throws Exception {
120         // 將解析結果存儲在HashMap中
121         Map<String, String> map = new HashMap<String, String>();
122         // 從request中取得輸入流
123         InputStream inputStream = request.getInputStream();
124         // 讀取輸入流
125         SAXReader reader = new SAXReader();
126         Document document = reader.read(inputStream);
127         // 獲得xml根元素
128         Element root = document.getRootElement();
129         // 獲得根元素的全部子節點
130         List<Element> elementList = root.elements();
131         // 遍歷全部子節點
132         for (Element e : elementList) {
133             map.put(e.getName(), e.getText());
134         }
135         // 釋放資源
136         inputStream.close();
137         inputStream = null;
138         return map;
139     }
140 }
View Code

 

經過對開發文檔的分析咱們能夠發現這些消息類型中,都會傳回來這些公共的字段如:web

    ToUserName(開發者微信號);服務器

    FromUserName(發送方賬 號,OPEN_ID);微信

    CreateTime(消息的建立時間);微信公衆平臺

    MsgType(消息類型);dom

    MsgId(消息ID)。maven

咱們把這些封裝成一個基類,而後 不一樣的部分,分別封裝爲各自的類而後繼承這個基類,提升代碼的重用性。ide

(一)消息實體基礎類 -- BaseMessage

 1 package com.webchat.entity.message;
 2 
 3 /**
 4  * 請求消息的公共字段類
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public abstract class BaseMessage {
10     // 開發者微信號
11     private String ToUserName;
12     // 發送方賬號(一個OpenID)
13     private String FromUserName;
14     // 消息建立時間 (整型)
15     private long CreateTime;
16     // 消息id,64位整型
17     private long MsgId;
18     /**
19      * 獲取 消息類型
20      *
21      * @return 消息類型
22      */
23     public abstract String getMsgType();
24 
25     public String getToUserName() {
26         return ToUserName;
27     }
28 
29     public void setToUserName(String toUserName) {
30         ToUserName = toUserName;
31     }
32 
33     public String getFromUserName() {
34         return FromUserName;
35     }
36 
37     public void setFromUserName(String fromUserName) {
38         FromUserName = fromUserName;
39     }
40 
41     public long getCreateTime() {
42         return CreateTime;
43     }
44 
45     public void setCreateTime(long createTime) {
46         CreateTime = createTime;
47     }
48     
49     public long getMsgId() {
50         return MsgId;
51     }
52 
53     public void setMsgId(long msgId) {
54         MsgId = msgId;
55     }
56 }
View Code

(二)普通消息類

   1,文本消息

 1 package com.webchat.entity.message;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 
 5 /**
 6  * 文本消息
 7  * @author Administrator
 8  *
 9  */
10 public class TextMessage extends BaseMessage {
11     //文本消息內容
12     private String Content;
13 
14     public String getContent() {
15         return Content;
16     }
17 
18     public void setContent(String content) {
19         Content = content;
20     }
21 
22     @Override
23     public String getMsgType() {
24         return MessageType.TEXT_MESSAGE.toString();
25     }
26 
27 }
View Code

   2,圖片消息

 1 package com.webchat.entity.message;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 /**
 5  * 圖片消息
 6  * @author Administrator
 7  *
 8  */
 9 public class ImageMessage extends BaseMessage{
10     // 圖片連接
11     private String PicUrl;
12     //圖片消息媒體id,能夠調用多媒體文件下載接口拉取數據。
13     private String MediaId;
14 
15     public String getPicUrl() {
16         return PicUrl;
17     }
18 
19     public void setPicUrl(String picUrl) {
20         PicUrl = picUrl;
21     }
22 
23     public String getMediaId() {
24         return MediaId;
25     }
26 
27     public void setMediaId(String mediaId) {
28         MediaId = mediaId;
29     }
30 
31     @Override
32     public String getMsgType() {
33         return MessageType.IMAGE_MESSAGE.toString();
34     }
35 
36 }
View Code

   3,語音消息

 1 package com.webchat.entity.message;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 
 5 /**
 6  * 語音消息
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class VoiceMessage extends BaseMessage {
12     // 語音消息媒體id,能夠調用多媒體文件下載接口拉取數據。
13     private String MediaId;
14     // 語音格式,如amr,speex等
15     private String Format;
16     // 語音識別結果,使用UTF8編碼
17     private String Recognition;
18 
19     public String getMediaId() {
20         return MediaId;
21     }
22 
23     public void setMediaId(String mediaId) {
24         MediaId = mediaId;
25     }
26 
27     public String getFormat() {
28         return Format;
29     }
30 
31     public void setFormat(String format) {
32         Format = format;
33     }
34 
35     public String getRecognition() {
36         return Recognition;
37     }
38 
39     public void setRecognition(String recognition) {
40         Recognition = recognition;
41     }
42 
43     @Override
44     public String getMsgType() {
45         return MessageType.VOICE_MESSAGE.toString();
46     }
47 
48 }
View Code

   4,視頻消息

 1 package com.webchat.entity.message;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 /**
 5  * 視頻消息
 6  * @author Administrator
 7  *
 8  */
 9 public class VideoMessage extends BaseMessage {
10     // 視頻消息媒體id,能夠調用多媒體文件下載接口拉取數據。
11     private String MediaId;
12     // 視頻消息 視頻消息縮略圖的媒體id,能夠調用多媒體文件下載接口拉取數據。
13     private String ThumbMediaId;
14 
15     public String getMediaId() {
16         return MediaId;
17     }
18 
19     public void setMediaId(String mediaId) {
20         MediaId = mediaId;
21     }
22 
23     public String getThumbMediaId() {
24         return ThumbMediaId;
25     }
26 
27     public void setThumbMediaId(String thumbMediaId) {
28         ThumbMediaId = thumbMediaId;
29     }
30 
31     @Override
32     public String getMsgType() {
33         return MessageType.VIDEO_MESSAGE.toString();
34     }
35 
36 }
View Code

   5,小視頻消息

 1 package com.webchat.entity.message;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 /**
 5  * 小視頻消息
 6  * @author Administrator
 7  *
 8  */
 9 public class ShortVideoInputMessage extends BaseMessage {
10     // 視頻消息媒體id,能夠調用多媒體文件下載接口拉取數據。
11     private String MediaId;
12     // 視頻消息 視頻消息縮略圖的媒體id,能夠調用多媒體文件下載接口拉取數據。
13     private String ThumbMediaId;
14 
15     public String getMediaId() {
16         return MediaId;
17     }
18 
19     public void setMediaId(String mediaId) {
20         MediaId = mediaId;
21     }
22 
23     public String getThumbMediaId() {
24         return ThumbMediaId;
25     }
26 
27     public void setThumbMediaId(String thumbMediaId) {
28         ThumbMediaId = thumbMediaId;
29     }
30 
31     @Override
32     public String getMsgType() {
33         return MessageType.SHORTVIDEO_MESSAGE.toString();
34     }
35 }
View Code

   6,地理位置消息

 1 package com.webchat.entity.message;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 
 5 /**
 6  * 地理位置消息
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class LocationMessage extends BaseMessage {
12     // 地理位置維度
13     private String Location_X;
14     // 地理位置經度
15     private String Location_Y;
16     // 地圖縮放大小
17     private Long Scale;
18     // 地理位置信息
19     private String Label;
20 
21     public String getLocation_X() {
22         return Location_X;
23     }
24 
25     public void setLocation_X(String location_X) {
26         Location_X = location_X;
27     }
28 
29     public String getLocation_Y() {
30         return Location_Y;
31     }
32 
33     public void setLocation_Y(String location_Y) {
34         Location_Y = location_Y;
35     }
36 
37     public Long getScale() {
38         return Scale;
39     }
40 
41     public void setScale(Long scale) {
42         Scale = scale;
43     }
44 
45     public String getLabel() {
46         return Label;
47     }
48 
49     public void setLabel(String label) {
50         Label = label;
51     }
52 
53     @Override
54     public String getMsgType() {
55         return MessageType.POSOTION_MESSAGE.toString();
56     }
57 
58 }
View Code

   7,連接消息

 1 package com.webchat.entity.message;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 
 5 /**
 6  * 連接消息
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class LinkMessage extends BaseMessage {
12     // 消息標題
13     private String Title;
14     // 消息描述
15     private String Description;
16     // 消息連接
17     private String Url;
18 
19     public String getTitle() {
20         return Title;
21     }
22 
23     public void setTitle(String title) {
24         Title = title;
25     }
26 
27     public String getDescription() {
28         return Description;
29     }
30 
31     public void setDescription(String description) {
32         Description = description;
33     }
34 
35     public String getUrl() {
36         return Url;
37     }
38 
39     public void setUrl(String url) {
40         Url = url;
41     }
42 
43     @Override
44     public String getMsgType() {
45         return MessageType.LINK_MESSAGE.toString();
46     }
47 
48 }
View Code

(二)封裝事件

點擊左側的「消息管理」----「接收事件推送」,在右側咱們能夠看到微信接收事件推送的目錄:關注/取消關注事件、掃描帶參數二維碼事件、上報地理位置事件、自定義菜單事件、點擊菜單拉取消息時的事件推送、點擊菜單跳轉連接時的事件推送;推送XML數據包 和消息文本的相似,詳情參考開發者文檔

提取公共字段建立基礎類

 1 package com.webchat.entity.event;
 2 
 3 /**
 4  * 事件消息
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public abstract class BaseEvent {
10     // 開發者微信號
11     private String ToUserName;
12     // 發送方賬號(一個OpenID)
13     private String FromUserName;
14     // 消息建立時間 (整型)
15     private long CreateTime;
16     // 消息類型
17     private String MsgType;
18     // 事件類型
19     private String Event;
20 
21     public String getEvent() {
22         return Event;
23     }
24 
25     public void setEvent(String event) {
26         Event = event;
27     }
28 
29     public String getToUserName() {
30         return ToUserName;
31     }
32 
33     public void setToUserName(String toUserName) {
34         ToUserName = toUserName;
35     }
36 
37     public String getFromUserName() {
38         return FromUserName;
39     }
40 
41     public void setFromUserName(String fromUserName) {
42         FromUserName = fromUserName;
43     }
44 
45     public long getCreateTime() {
46         return CreateTime;
47     }
48 
49     public void setCreateTime(long createTime) {
50         CreateTime = createTime;
51     }
52 
53     public String getMsgType() {
54         return MsgType;
55     }
56 
57     public void setMsgType(String msgType) {
58         MsgType = msgType;
59     }
60 }
View Code

事件封裝

   1,關注/取消事件

1 package com.webchat.entity.event;
2 /**
3  * 關注取消事件
4  * @author Administrator
5  *
6  */
7 public class SubscribeEvent extends BaseEvent{
8 
9 }
View Code

   2,掃描參數帶二維碼事件

 1 package com.webchat.entity.event;
 2 
 3 /**
 4  * 掃描帶參數二維碼事件
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public class QRCodeEvent extends BaseEvent {
10     // 事件KEY值
11     private String EventKey;
12     // 用於換取二維碼圖片
13     private String Ticket;
14 
15     public String getEventKey() {
16         return EventKey;
17     }
18 
19     public void setEventKey(String eventKey) {
20         EventKey = eventKey;
21     }
22 
23     public String getTicket() {
24         return Ticket;
25     }
26 
27     public void setTicket(String ticket) {
28         Ticket = ticket;
29     }
30 
31 }
View Code

   3,上報地理位置事件

 1 package com.webchat.entity.event;
 2 
 3 /**
 4  * 上報地理位置事件
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public class LocationEvent extends BaseEvent {
10     // 地理位置緯度
11     private String Latitude;
12     // 地理位置經度
13     private String Longitude;
14     // 地理位置精度
15     private String Precision;
16 
17     public String getLatitude() {
18         return Latitude;
19     }
20 
21     public void setLatitude(String latitude) {
22         Latitude = latitude;
23     }
24 
25     public String getLongitude() {
26         return Longitude;
27     }
28 
29     public void setLongitude(String longitude) {
30         Longitude = longitude;
31     }
32 
33     public String getPrecision() {
34         return Precision;
35     }
36 
37     public void setPrecision(String precision) {
38         Precision = precision;
39     }
40 
41 }
View Code

   4,自定義菜單事件

 1 package com.webchat.entity.event;
 2 
 3 /**
 4  * 自定義菜單事件
 5  * @author Administrator
 6  *
 7  */
 8 public class MenuEvent extends BaseEvent {
 9     // 事件KEY值,與自定義菜單接口中KEY值對應
10     private String EventKey;
11     
12     public String getEventKey() {
13         return EventKey;
14     }
15 
16     public void setEventKey(String eventKey) {
17         EventKey = eventKey;
18     }
19 
20 }
View Code

(三)回覆消息的分類及實體的封裝

點擊左側的「消息管理」----「被動回覆用戶消息」,當用戶發送消息給公衆號時(或某些特定的用戶操做引起的事件推送時),會產生一個POST請求,開發者能夠在響應包(Get)中返回特定XML結構,來對該消息進行響應(現支持回覆文本、圖片、圖文、語音、視頻、音樂)。嚴格來講,發送被動響應消息其實並非一種接口,而是對微信服務器發過來消息的一次回覆。

微信服務器在將用戶的消息發給公衆號的開發者服務器地址(開發者中心處配置)後,微信服務器在五秒內收不到響應會斷掉鏈接,而且從新發起請求,總共重試三次,若是在調試中,發現用戶沒法收到響應的消息,能夠檢查是否消息處理超時。關於重試的消息排重,有msgid的消息推薦使用msgid排重。事件類型消息推薦使用FromUserName + CreateTime 排重。詳情請查看開發者文檔

一樣,把消息回覆中定義的全部消息都有的字段提取出來,封裝成一個公共類,

響應消息的基類BaseOutMessage

 1 package com.webchat.entity.output;
 2 
 3 /**
 4  * 回覆消息的公共字段類
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public abstract class BaseOutMessage {
10     // 接收方賬號(收到的OpenID)
11     private String ToUserName;
12     // 開發者微信號
13     private String FromUserName;
14     // 消息建立時間 (整型)
15     private long CreateTime;
16 
17     // 獲取消息類型
18     public abstract String getMsgType();
19     
20     public String getToUserName() {
21         return ToUserName;
22     }
23 
24     public void setToUserName(String toUserName) {
25         ToUserName = toUserName;
26     }
27 
28     public String getFromUserName() {
29         return FromUserName;
30     }
31 
32     public void setFromUserName(String fromUserName) {
33         FromUserName = fromUserName;
34     }
35 
36     public long getCreateTime() {
37         return CreateTime;
38     }
39 
40     public void setCreateTime(long createTime) {
41         CreateTime = createTime;
42     }
43 
44 }
View Code

普通消息回覆實體實現

   1,回覆文本消息

 1 package com.webchat.entity.output;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 
 5 /**
 6  * 文本回復消息
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class TextMessage extends BaseOutMessage {
12     // 文本消息
13     private String Content;
14 
15     public String getContent() {
16         return Content;
17     }
18 
19     public void setContent(String content) {
20         Content = content;
21     }
22 
23     @Override
24     public String getMsgType() {
25         return MessageType.RESP_MESSAGE_TYPE_TEXT.toString();
26     }
27 }
View Code

   2,回覆圖片消息

 1 package com.webchat.entity.output;
 2 
 3 
 4 /**
 5  * 圖片回覆消息
 6  * @author Administrator
 7  *
 8  */
 9 public class Image{
10     //經過上傳多媒體文件,獲得的id
11     private String MediaId;
12     
13     public String getMediaId() {
14         return MediaId;
15     }
16 
17     public void setMediaId(String mediaId) {
18         MediaId = mediaId;
19     }
20 }
View Code
 1 package com.webchat.entity.output;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 /**
 5  * 圖片輸出內容
 6  * @author Administrator
 7  *
 8  */
 9 public class ImageOutputMessage extends BaseOutMessage{
10     private Image Image;
11     public Image getImage() {
12         return Image;
13     }
14     public void setImage(Image image) {
15         Image = image;
16     }
17     @Override
18     public String getMsgType() {
19         return MessageType.RESP_MESSAGE_TYPE_IMAGE.toString();
20     }
21 }
View Code

   3,回覆語音消息

 1 package com.webchat.entity.output;
 2 /**
 3  * 語音model
 4  * @author Administrator
 5  *
 6  */
 7 public class Voice {
 8     // 媒體文件id
 9     private String MediaId;
10 
11     public String getMediaId() {
12         return MediaId;
13     }
14 
15     public void setMediaId(String mediaId) {
16         MediaId = mediaId;
17     }
18     
19 }
View Code
 1 package com.webchat.entity.output;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 /**
 5  * 語音回覆消息
 6  * @author Administrator
 7  *
 8  */
 9 public class VoiceOutputMessage extends BaseOutMessage{
10     private Voice voice;
11     
12     public Voice getVoice() {
13         return voice;
14     }
15 
16     public void setVoice(Voice voice) {
17         this.voice = voice;
18     }
19 
20     @Override
21     public String getMsgType() {
22         return MessageType.RESP_MESSAGE_TYPE_VOICE.toString();
23     }
24 }
View Code

   4,回覆視頻消息

 1 package com.webchat.entity.output;
 2 /**
 3  * 視頻model
 4  * @author Administrator
 5  *
 6  */
 7 public class Video {
 8     // 媒體文件id
 9     private String MediaId;
10     // 縮略圖的媒體id
11     private String ThumbMediaId;
12     //視頻消息的標題
13     private String Title; 
14     //視頻消息的描述
15     private String Description;
16     public String getMediaId() {
17         return MediaId;
18     }
19 
20     public void setMediaId(String mediaId) {
21         MediaId = mediaId;
22     }
23 
24     public String getThumbMediaId() {
25         return ThumbMediaId;
26     }
27 
28     public void setThumbMediaId(String thumbMediaId) {
29         ThumbMediaId = thumbMediaId;
30     }
31 
32     public String getTitle() {
33         return Title;
34     }
35 
36     public void setTitle(String title) {
37         Title = title;
38     }
39 
40     public String getDescription() {
41         return Description;
42     }
43 
44     public void setDescription(String description) {
45         Description = description;
46     }
47     
48 }
View Code
 1 package com.webchat.entity.output;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 
 5 /**
 6  * 回覆視頻消息
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class VideoOutPutMessage extends BaseOutMessage {
12     private Video Video;
13 
14     public Video getVideo() {
15         return Video;
16     }
17 
18     public void setVideo(Video video) {
19         Video = video;
20     }
21 
22     @Override
23     public String getMsgType() {
24         return MessageType.RESP_MESSAGE_TYPE_VIDEO.toString();
25     }
26 }
View Code

   5,回覆音樂消息

 1 package com.webchat.entity.output;
 2 /**
 3  * 回覆音樂消息中的音樂對象
 4  * @author Administrator
 5  *
 6  */
 7 public class Music {
 8     // 音樂標題
 9     private String Title;
10     // 音樂描述
11     private String Description;
12     // 音樂連接
13     private String MusicUrl;
14     // 高質量音樂連接,WIFI環境優先使用該連接播放音樂
15     private String HQMusicUrl;
16     // 縮略圖的媒體id,經過上傳多媒體文件獲得的id
17     private String ThumbMediaId;
18 
19     public String getTitle() {
20         return Title;
21     }
22 
23     public void setTitle(String title) {
24         Title = title;
25     }
26 
27     public String getDescription() {
28         return Description;
29     }
30 
31     public void setDescription(String description) {
32         Description = description;
33     }
34 
35     public String getMusicUrl() {
36         return MusicUrl;
37     }
38 
39     public void setMusicUrl(String musicUrl) {
40         MusicUrl = musicUrl;
41     }
42 
43     public String getHQMusicUrl() {
44         return HQMusicUrl;
45     }
46 
47     public void setHQMusicUrl(String hQMusicUrl) {
48         HQMusicUrl = hQMusicUrl;
49     }
50 
51     public String getThumbMediaId() {
52         return ThumbMediaId;
53     }
54 
55     public void setThumbMediaId(String thumbMediaId) {
56         ThumbMediaId = thumbMediaId;
57     }
58 
59 }
View Code
 1 package com.webchat.entity.output;
 2 
 3 import com.webchat.util.weixin.MessageType;
 4 /**
 5  * 回覆音樂消息
 6  * @author Administrator
 7  *
 8  */
 9 public class MusicOutputMessage extends BaseOutMessage {
10     private Music Music;
11 
12     public Music getMusic() {
13         return Music;
14     }
15 
16     public void setMusic(Music music) {
17         Music = music;
18     }
19 
20     @Override
21     public String getMsgType() {
22         return MessageType.RESP_MESSAGE_TYPE_MUSIC.toString();
23     }
24 }
View Code

   6,回覆圖文消息

 1 package com.webchat.entity.output;
 2 /**
 3  * 圖文消息實體類對象
 4  * @author Administrator
 5  *
 6  */
 7 public class Articles {
 8     private String Title;
 9     // 圖文消息描述
10     private String Description;
11     // 圖片連接,支持JPG、PNG格式,較好的效果爲大圖640*320,小圖80*80
12     private String PicUrl;
13     // 點擊圖文消息跳轉連接
14     private String Url;
15 
16     public String getTitle() {
17         return Title;
18     }
19 
20     public void setTitle(String title) {
21         Title = title;
22     }
23 
24     public String getDescription() {
25         return Description;
26     }
27 
28     public void setDescription(String description) {
29         Description = description;
30     }
31 
32     public String getPicUrl() {
33         return PicUrl;
34     }
35 
36     public void setPicUrl(String picUrl) {
37         PicUrl = picUrl;
38     }
39 
40     public String getUrl() {
41         return Url;
42     }
43 
44     public void setUrl(String url) {
45         Url = url;
46     }
47 }
View Code
 1 package com.webchat.entity.output;
 2 
 3 import java.util.List;
 4 import com.webchat.util.weixin.MessageType;
 5 
 6 /**
 7  * 提供了獲取多條圖文消息信息
 8  * 
 9  * @author Administrator
10  *
11  */
12 public class NewsOutputMessage extends BaseOutMessage {
13     // 圖文消息個數,限制爲10條之內
14     private int ArticleCount;
15     // 多條圖文消息信息,默認第一個item爲大圖
16     private List<Articles> Articles;
17 
18     public int getArticleCount() {
19         return ArticleCount;
20     }
21 
22     public void setArticleCount(int articleCount) {
23         ArticleCount = articleCount;
24     }
25 
26     public List<Articles> getArticles() {
27         return Articles;
28     }
29 
30     public void setArticles(List<Articles> articles) {
31         Articles = articles;
32     }
33 
34     @Override
35     public String getMsgType() {
36         return MessageType.RESP_MESSAGE_TYPE_NEWS.toString();
37     }
38 }
View Code

至此,咱們全部的內容已經封裝完成

接下來會更新:微信服務器 post 消息體的接收及消息處理的內容

若是在操做過程當中有問題,歡迎隨時討論^.^

百度雲連接:https://pan.baidu.com/s/1xQIAl14_9JKJi1BsFe7yvw    密碼:ybnv

備註:個人是maven項目,此連接只爲分享封裝的實體類,若是項目中出現錯誤,可不用理會,只拿本身想要的東西便可

 

 

 

 

 

 

 

 

其餘文章關聯

(一)Java開發微信公衆號(一)---初識微信公衆號以及環境搭建

(二)Java開發微信公衆號(二)---開啓開發者模式,接入微信公衆平臺開發

(三)Java開發微信公衆號(三)---微信服務器請求消息,響應消息,事件消息以及工具處理類的封裝

(四)Java開發微信公衆號(四)---微信服務器post消息體的接收及消息的處理

相關文章
相關標籤/搜索