這篇文章給大家分享的是有關(guān)如何使用java實現(xiàn)微信分類接收消息以及創(chuàng)建實體的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
十年的元江縣網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整元江縣建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“元江縣網(wǎng)站設(shè)計”,“元江縣網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
(一)消息實體基礎(chǔ)類
package com.cuiyongzhi.wechat.message.req;
/**
* ClassName: BaseMessage
* @Description: 微信請求消息基本類
* @author dapengniao
* @date 2016年3月7日 下午3:03:59
*/
public class BaseMessage {
// 開發(fā)者微信號
private String ToUserName;
// 發(fā)送方帳號(一個OpenID)
private String FromUserName;
// 消息創(chuàng)建時間 (整型)
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實體
①圖片消息
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;
}
}②連接消息
package com.cuiyongzhi.wechat.message.req;
/**
* ClassName: LinkMessage
* @Description: 連接消息
* @author dapengniao
* @date 2016年3月7日 下午3:05:48
*/
public class LinkMessage extends BaseMessage {
// 消息標題
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;
}
}③地理位置消息
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;
}
}④文本消息
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;
}
}⑤視頻/小視屏消息
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;
}
}⑥語音消息
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),分別做普通消息處理和事件消息處理!
①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;
}
}②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)) { //位置上報事件
System.out.println("==============這是位置上報事件!");
}
if (map.get("Event").equals(MessageUtil.EVENT_TYPE_CLICK)) { //自定義菜單點擊事件
System.out.println("==============這是自定義菜單點擊事件!");
}
if (map.get("Event").equals(MessageUtil.EVENT_TYPE_VIEW)) { //自定義菜單View事件
System.out.println("==============這是自定義菜單View事件!");
}
return null;
}
}這個時候我們需要把我們的消息入口【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ù),返回驗證參數(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("這里存在非法請求!");
}
} 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); //進入事件處理
}else{
MsgDispatcher.processMessage(map); //進入消息處理
}
}catch(Exception e){
logger.error(e,e);
}
}
}最后我們運行成功項目之后我們可以通過發(fā)送不同類型的消息來驗證我們的消息分類的正確性,如下圖所示:

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

感謝各位的閱讀!關(guān)于“如何使用java實現(xiàn)微信分類接收消息以及創(chuàng)建實體”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
名稱欄目:如何使用java實現(xiàn)微信分類接收消息以及創(chuàng)建實體
文章來源:http://www.chinadenli.net/article32/gcsjsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站設(shè)計公司、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航、微信小程序、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)