鍍金池/ 教程/ Java/ Java 微信公眾平臺(tái)開發(fā)(三)--接收消息的分類及實(shí)體的創(chuàng)建
Java 微信公眾平臺(tái)開發(fā)(八)--多媒體消息回復(fù)
Java 微信公眾平臺(tái)開發(fā)(四)--回復(fù)消息的分類及實(shí)體的創(chuàng)建
Mybatis 工具 Generator
Java 微信公眾平臺(tái)開發(fā)(十一)--開發(fā)中微信公眾平臺(tái)/開放平臺(tái)/商戶平臺(tái)的關(guān)聯(lián)
微信開發(fā)準(zhǔn)備(二)--springmvc+mybatis 項(xiàng)目結(jié)構(gòu)的搭建
 Java 微信公眾平臺(tái)開發(fā)(十二)--微信用戶信息的獲取
Java 微信公眾平臺(tái)開發(fā)(十五)--微信 JSSDK 的使用
微信開發(fā)準(zhǔn)備(三)--框架以及工具的基本使用
Java 微信公眾平臺(tái)開發(fā)(十三)--微信 JSSDK 中 Config 配置
Java 微信公眾平臺(tái)開發(fā)(一)--接入微信公眾平臺(tái)
Java 微信公眾平臺(tái)開發(fā)(十四)【番外篇】--微信 web 開發(fā)者工具使用
Java 微信公眾平臺(tái)開發(fā)【番外篇】(七)--公眾平臺(tái)測(cè)試帳號(hào)的申請(qǐng)
微信開發(fā)準(zhǔn)備(一)--Maven 倉庫管理新建 WEB 項(xiàng)目
Java 微信公眾平臺(tái)開發(fā)(三)--接收消息的分類及實(shí)體的創(chuàng)建
Java 微信公眾平臺(tái)開發(fā)(九)--關(guān)鍵字回復(fù)以及客服接口實(shí)現(xiàn)(該公眾號(hào)暫時(shí)無法提供服務(wù)解決方案)
微信開發(fā)準(zhǔn)備(四)--nat123 內(nèi)網(wǎng)地址公網(wǎng)映射實(shí)現(xiàn)
Java 微信公眾平臺(tái)開發(fā)(五)--文本及圖文消息回復(fù)的實(shí)現(xiàn)
Java 微信公眾平臺(tái)開發(fā)(十)--微信自定義菜單的創(chuàng)建實(shí)現(xiàn)
Java 微信公眾平臺(tái)開發(fā)(六)--微信開發(fā)中的 token 獲取
Java 微信公眾平臺(tái)開發(fā)(二)--微信服務(wù)器 post 消息體的接收

Java 微信公眾平臺(tái)開發(fā)(三)--接收消息的分類及實(shí)體的創(chuàng)建

前面一篇有說道應(yīng)用服務(wù)器和騰訊服務(wù)器是通過消息進(jìn)行通訊的,并簡單介紹了微信端 post 的消息類型,這里我們將建立消息實(shí)體以方便我們后面的使用!

(一)消息實(shí)體基礎(chǔ)類

package com.cuiyongzhi.wechat.message.req;

/**
 * ClassName: BaseMessage
 * @Description: 微信請(qǐng)求消息基本類
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 3:03:59
 */
public class BaseMessage {
    // 開發(fā)者微信號(hào)
    private String ToUserName;
    // 發(fā)送方帳號(hào)(一個(gè) OpenID)
    private String FromUserName;
    // 消息創(chuàng)建時(shí)間 (整型)
    private long CreateTime;
    // 消息類型(text/image/location/link/video/shortvideo)
    private String MsgType;
    // 消息 id,64 位整型
    private long 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 long getMsgId() {
        return MsgId;
    }

    public void setMsgId(long msgId) {
        MsgId = msgId;
    }
}

(二)普通消息 pojo 實(shí)體

1.圖片消息

package com.cuiyongzhi.wechat.message.req;

/**
 * ClassName: ImageMessage
 * @Description: 圖片消息
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 3:04:52
 */
public class ImageMessage extends BaseMessage {
    // 圖片鏈接
    private String PicUrl;

    public String getPicUrl() {
        return PicUrl;
    }

    public void setPicUrl(String picUrl) {
        PicUrl = picUrl;
    }
}

2.連接消息

package com.cuiyongzhi.wechat.message.req;

/**
 * ClassName: LinkMessage
 * @Description: 連接消息
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 3:05:48
 */
public class LinkMessage extends BaseMessage {
    // 消息標(biāo)題
    private String Title;
    // 消息描述
    private String Description;
    // 消息鏈接
    private String Url;

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public String getUrl() {
        return Url;
    }

    public void setUrl(String url) {
        Url = url;
    }
}

3.地理位置消息

package com.cuiyongzhi.wechat.message.req;

/**
 * ClassName: LocationMessage
 * @Description: 地理位置消息
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 3:06:10
 */
public class LocationMessage extends BaseMessage {  
    // 地理位置維度   
    private String Location_X;  
    // 地理位置經(jīng)度   
    private String Location_Y;  
    // 地圖縮放大小   
    private String Scale;  
    // 地理位置信息   
    private String Label;  

    public String getLocation_X() {  
        return Location_X;  
    }  

    public void setLocation_X(String location_X) {  
        Location_X = location_X;  
    }  

    public String getLocation_Y() {  
        return Location_Y;  
    }  

    public void setLocation_Y(String location_Y) {  
        Location_Y = location_Y;  
    }  

    public String getScale() {  
        return Scale;  
    }  

    public void setScale(String scale) {  
        Scale = scale;  
    }  

    public String getLabel() {  
        return Label;  
    }  

    public void setLabel(String label) {  
        Label = label;  
    }  
}

4.文本消息

package com.cuiyongzhi.wechat.message.req;

/**
 * ClassName: TextMessage
 * @Description: 文本消息
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 3:06:40
 */
public class TextMessage extends BaseMessage {  
    // 消息內(nèi)容   
    private String Content;  

    public String getContent() {  
        return Content;  
    }  

    public void setContent(String content) {  
        Content = content;  
    }  
}

5.視頻/小視屏消息

package com.cuiyongzhi.wechat.message.req;

/**
 * ClassName: VideoMessage
 * @Description: 視頻/小視屏消息
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 3:12:51
 */
public class VideoMessage extends BaseMessage {

    private String MediaId; // 視頻消息媒體 id,可以調(diào)用多媒體文件下載接口拉取數(shù)據(jù)
    private String ThumbMediaId; // 視頻消息縮略圖的媒體 id,可以調(diào)用多媒體文件下載接口拉取數(shù)據(jù)

    public String getMediaId() {
        return MediaId;
    }

    public void setMediaId(String mediaId) {
        MediaId = mediaId;
    }

    public String getThumbMediaId() {
        return ThumbMediaId;
    }

    public void setThumbMediaId(String thumbMediaId) {
        ThumbMediaId = thumbMediaId;
    }

}

6.語音消息

package com.cuiyongzhi.wechat.message.req;

/**
 * ClassName: VoiceMessage
 * @Description: 語音消息
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 3:07:10
 */
public class VoiceMessage extends BaseMessage {  
    // 媒體 ID   
    private String MediaId;  
    // 語音格式   
    private String Format;  

    public String getMediaId() {  
        return MediaId;  
    }  

    public void setMediaId(String mediaId) {  
        MediaId = mediaId;  
    }  

    public String getFormat() {  
        return Format;  
    }  

    public void setFormat(String format) {  
        Format = format;  
    }  
}

(三)消息分類處理

按照上面收到想消息類別分別做不同的分發(fā)處理,這里我們建立了自己的業(yè)務(wù)分發(fā)器(EventDispatcher、MsgDispatcher),分別做普通消息處理和事件消息處理!

1.MsgDispatcher.java——用于普通消息的業(yè)務(wù)分發(fā)處理

package com.cuiyongzhi.wechat.dispatcher;

import java.util.Map;

import com.cuiyongzhi.wechat.util.MessageUtil;

/**
 * ClassName: MsgDispatcher
 * @Description: 消息業(yè)務(wù)處理分發(fā)器
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 4:04:21
 */
public class MsgDispatcher {
    public static String processMessage(Map<String, String> map) {
        if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) { // 文本消息
            System.out.println("==============這是文本消息!");
        }

        if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) { // 圖片消息
            System.out.println("==============這是圖片消息!");
        }

        if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) { // 鏈接消息
            System.out.println("==============這是鏈接消息!");
        }

        if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) { // 位置消息
            System.out.println("==============這是位置消息!");
        }

        if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)) { // 視頻消息
            System.out.println("==============這是視頻消息!");
        }

        if (map.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) { // 語音消息
            System.out.println("==============這是語音消息!");
        }

        return null;
    }
}

2.EventDispatcher.java——事件消息的業(yè)務(wù)分發(fā)處理

package com.cuiyongzhi.wechat.dispatcher;

import java.util.Map;

import com.cuiyongzhi.wechat.util.MessageUtil;

/**
 * ClassName: EventDispatcher
 * @Description: 事件消息業(yè)務(wù)分發(fā)器
 * @author dapengniao
 * @date 2016 年 3 月 7 日 下午 4:04:41
 */
public class EventDispatcher {
    public static String processEvent(Map<String, String> map) {
        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) { //關(guān)注事件
            System.out.println("==============這是關(guān)注事件!");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) { //取消關(guān)注事件
            System.out.println("==============這是取消關(guān)注事件!");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_SCAN)) { //掃描二維碼事件
            System.out.println("==============這是掃描二維碼事件!");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_LOCATION)) { //位置上報(bào)事件
            System.out.println("==============這是位置上報(bào)事件!");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_CLICK)) { //自定義菜單點(diǎn)擊事件
            System.out.println("==============這是自定義菜單點(diǎn)擊事件!");
        }

        if (map.get("Event").equals(MessageUtil.EVENT_TYPE_VIEW)) { //自定義菜單 View 事件
            System.out.println("==============這是自定義菜單 View 事件!");
        }

        return null;
    }
}

這個(gè)時(shí)候我們需要把我們的消息入口【W(wǎng)echatSecurity.java】中的 post 方法做些修改,最終結(jié)果如下:

package com.cuiyongzhi.wechat.controller;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cuiyongzhi.wechat.dispatcher.EventDispatcher;
import com.cuiyongzhi.wechat.dispatcher.MsgDispatcher;
import com.cuiyongzhi.wechat.util.MessageUtil;
import com.cuiyongzhi.wechat.util.SignUtil;

@Controller
@RequestMapping("/wechat")
public class WechatSecurity {
    private static Logger logger = Logger.getLogger(WechatSecurity.class);

    /**
     * 
     * @Description: 用于接收 get 參數(shù),返回驗(yàn)證參數(shù)
     * @param @param request
     * @param @param response
     * @param @param signature
     * @param @param timestamp
     * @param @param nonce
     * @param @param echostr
     * @author dapengniao
     * @date 2016 年 3 月 4 日 下午 6:20:00
     */
    @RequestMapping(value = "security", method = RequestMethod.GET)
    public void doGet(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "signature", required = true) String signature,
            @RequestParam(value = "timestamp", required = true) String timestamp,
            @RequestParam(value = "nonce", required = true) String nonce,
            @RequestParam(value = "echostr", required = true) String echostr) {
        try {
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                PrintWriter out = response.getWriter();
                out.print(echostr);
                out.close();
            } else {
                logger.info("這里存在非法請(qǐng)求!");
            }
        } catch (Exception e) {
            logger.error(e, e);
        }
    }

    /**
     * @Description: 接收微信端消息處理并做分發(fā)
     * @param @param request
     * @param @param response   
     * @author dapengniao
     * @date 2016 年 3 月 7 日 下午 4:06:47
     */
    @RequestMapping(value = "security", method = RequestMethod.POST)
    public void DoPost(HttpServletRequest request,HttpServletResponse response) {
        try{
            Map<String, String> map=MessageUtil.parseXml(request);
            String msgtype=map.get("MsgType");
            if(MessageUtil.REQ_MESSAGE_TYPE_EVENT.equals(msgtype)){
                EventDispatcher.processEvent(map); //進(jìn)入事件處理
            }else{
                MsgDispatcher.processMessage(map); //進(jìn)入消息處理
            }
        }catch(Exception e){
            logger.error(e,e);
        }
    }
}

最后我們運(yùn)行成功項(xiàng)目之后我們可以通過發(fā)送不同類型的消息來驗(yàn)證我們的消息分類的正確性,如下圖所示:

http://wiki.jikexueyuan.com/project/java-wechat/images/20.png" alt="" />

新建了這么多文件,最后來看下我們的整個(gè)項(xiàng)目的目錄結(jié)構(gòu):

http://wiki.jikexueyuan.com/project/java-wechat/images/21.png" alt="" />

前面講述的都是消息的接收,那么下一篇起將講述【回復(fù)消息的分類及實(shí)體的創(chuàng)建】,感謝你的翻閱,如有疑問可以留言一起討論!