标签:注意 ssr ring bool cep 教程 mat space 播放
https://www.iteye.com/blog/tuposky-2017429
前面两章已经介绍了如何接入微信公众平台,这一章说说消息的接收和发送
可以先了解公众平台的消息api接口(接收消息,发送消息)
http://mp.weixin.qq.com/wiki/index.php
接收消息
当普通微信用户向公众账号发消息时,微信服务器将POST消息的XML数据包到开发者填写的URL上。
http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E6%99%AE%E9%80%9A%E6%B6%88%E6%81%AF
接收的消息类型有6种,分别为:
可以根据官方的api提供的字段建立对应的实体类
如:文本消息
有很多属性是所有消息类型都需要的,可以把这些信息提取出来建立一个基类
- package com.ifp.weixin.entity.Message.req;
-
- public class BaseMessage {
-
- private String ToUserName;
-
- private String FromUserName;
-
- private long CreateTime;
-
-
- private String MsgType;
-
-
- 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;
- }
-
- }
接收的文本消息
- package com.ifp.weixin.entity.Message.req;
-
- public class TextMessage extends BaseMessage {
-
- private String Content;
-
- public String getContent() {
- return Content;
- }
-
- public void setContent(String content) {
- Content = content;
- }
- }
接收的图片消息
- package com.ifp.weixin.entity.Message.req;
-
- public class ImageMessage extends BaseMessage{
-
- private String picUrl;
-
- public String getPicUrl() {
- return picUrl;
- }
-
- public void setPicUrl(String picUrl) {
- this.picUrl = picUrl;
- }
-
- }
接收的链接消息
- package com.ifp.weixin.entity.Message.req;
-
-
- 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.ifp.weixin.entity.Message.req;
-
- public class VoiceMessage extends BaseMessage {
-
- 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;
- }
-
- }
接收的地理位置消息
- package com.ifp.weixin.entity.Message.req;
-
-
- public class LocationMessage extends BaseMessage {
-
- private String Location_X;
-
- 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;
- }
-
- }
发送被动响应消息
对于每一个POST请求,开发者在响应包(Get)中返回特定XML结构,对该消息进行响应(现支持回复文本、图片、图文、语音、视频、音乐)。请注意,回复图片等多媒体消息时需要预先上传多媒体文件到微信服务器,只支持认证服务号。
同样,建立响应消息的对应实体类
也把公共的属性提取出来,封装成基类
响应消息的基类
- package com.ifp.weixin.entity.Message.resp;
-
- public class BaseMessage {
-
-
- private String ToUserName;
-
- private String FromUserName;
-
- private long CreateTime;
-
-
- private String MsgType;
-
-
- private int FuncFlag;
-
- 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 int getFuncFlag() {
- return FuncFlag;
- }
-
- public void setFuncFlag(int funcFlag) {
- FuncFlag = funcFlag;
- }
- }
响应文本消息
- package com.ifp.weixin.entity.Message.resp;
-
-
- public class TextMessage extends BaseMessage {
-
- private String Content;
-
- public String getContent() {
- return Content;
- }
-
- public void setContent(String content) {
- Content = content;
- }
- }
响应图文消息
- package com.ifp.weixin.entity.Message.resp;
-
- import java.util.List;
-
- public class NewsMessage extends BaseMessage {
-
- private int ArticleCount;
-
- private List<Article> Articles;
-
- public int getArticleCount() {
- return ArticleCount;
- }
-
- public void setArticleCount(int articleCount) {
- ArticleCount = articleCount;
- }
-
- public List<Article> getArticles() {
- return Articles;
- }
-
- public void setArticles(List<Article> articles) {
- Articles = articles;
- }
- }
图文消息的定义
- package com.ifp.weixin.entity.Message.resp;
-
- public class Article {
-
- private String Title;
-
-
- private String Description;
-
-
- private String PicUrl;
-
-
- private String Url;
-
- public String getTitle() {
- return Title;
- }
-
- public void setTitle(String title) {
- Title = title;
- }
-
- public String getDescription() {
- return null == Description ? "" : Description;
- }
-
- public void setDescription(String description) {
- Description = description;
- }
-
- public String getPicUrl() {
- return null == PicUrl ? "" : PicUrl;
- }
-
- public void setPicUrl(String picUrl) {
- PicUrl = picUrl;
- }
-
- public String getUrl() {
- return null == Url ? "" : Url;
- }
-
- public void setUrl(String url) {
- Url = url;
- }
-
- }
响应音乐消息
- package com.ifp.weixin.entity.Message.resp;
-
-
-
- public class MusicMessage extends BaseMessage {
-
- private Music Music;
-
- public Music getMusic() {
- return Music;
- }
-
- public void setMusic(Music music) {
- Music = music;
- }
- }
音乐消息的定义
- package com.ifp.weixin.entity.Message.resp;
-
- public class Music {
-
- private String Title;
-
-
- private String Description;
-
-
- private String MusicUrl;
-
-
- private String HQMusicUrl;
-
- 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 getMusicUrl() {
- return MusicUrl;
- }
-
- public void setMusicUrl(String musicUrl) {
- MusicUrl = musicUrl;
- }
-
- public String getHQMusicUrl() {
- return HQMusicUrl;
- }
-
- public void setHQMusicUrl(String musicUrl) {
- HQMusicUrl = musicUrl;
- }
-
- }
构建好之后的项目结构图为
到这里,请求消息和响应消息的实体类都定义好了
解析请求消息
用户向微信公众平台发送消息后,微信公众平台会通过post请求发送给我们。
上一章中WeixinController 类的post方法我们空着
现在我们要在这里处理用户请求了。
因为微信的发送和接收都是用xml格式的,所以我们需要处理请求过来的xml格式。
发送的时候也需要转化成xml格式再发送给微信,所以封装了消息处理的工具类,用到dome4j和xstream两个jar包
接下来在处理业务逻辑,建立一个接收并响应消息的service类,并针对用户输入的1或2回复不同的信息给用户
- package com.ifp.weixin.biz.core.impl;
-
- import java.util.Date;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
-
- import org.apache.log4j.Logger;
- import org.springframework.stereotype.Service;
-
- import com.ifp.weixin.biz.core.CoreService;
- import com.ifp.weixin.constant.Constant;
- import com.ifp.weixin.entity.Message.resp.TextMessage;
- import com.ifp.weixin.util.MessageUtil;
-
- @Service("coreService")
- public class CoreServiceImpl implements CoreService{
-
- public static Logger log = Logger.getLogger(CoreServiceImpl.class);
-
-
- @Override
- public String processRequest(HttpServletRequest request) {
- String respMessage = null;
- try {
-
- Map<String, String> requestMap = MessageUtil.parseXml(request);
-
-
- String fromUserName = requestMap.get("FromUserName");
-
- String toUserName = requestMap.get("ToUserName");
-
- String msgType = requestMap.get("MsgType");
-
- TextMessage textMessage = new TextMessage();
- textMessage.setToUserName(fromUserName);
- textMessage.setFromUserName(toUserName);
- textMessage.setCreateTime(new Date().getTime());
- textMessage.setMsgType(Constant.RESP_MESSAGE_TYPE_TEXT);
- textMessage.setFuncFlag(0);
-
- if (msgType.equals(Constant.REQ_MESSAGE_TYPE_TEXT)) {
-
- String content = requestMap.get("Content");
-
- if ("1".equals(content)) {
- textMessage.setContent("1是很好的");
-
- respMessage = MessageUtil.textMessageToXml(textMessage);
- }else if ("2".equals(content)) {
- textMessage.setContent("我不是2货");
-
- respMessage = MessageUtil.textMessageToXml(textMessage);
- }
- }
-
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- return respMessage;
- }
-
-
- }
接下来在controller里面的post方法里面调用即可
WeixinController类的完整代码
- package com.ifp.weixin.controller;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
-
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
-
- import com.ifp.weixin.biz.core.CoreService;
- import com.ifp.weixin.util.SignUtil;
-
- @Controller
- @RequestMapping("/weixinCore")
- public class WeixinController {
-
- @Resource(name="coreService")
- private CoreService coreService;
-
- @RequestMapping(method = RequestMethod.GET)
- public void get(HttpServletRequest request, HttpServletResponse response) {
-
- String signature = request.getParameter("signature");
-
- String timestamp = request.getParameter("timestamp");
-
- String nonce = request.getParameter("nonce");
-
- String echostr = request.getParameter("echostr");
-
- PrintWriter out = null;
- try {
- out = response.getWriter();
-
- if (SignUtil.checkSignature(signature, timestamp, nonce)) {
- out.print(echostr);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- out.close();
- out = null;
- }
- }
-
- @RequestMapping(method = RequestMethod.POST)
- public void post(HttpServletRequest request, HttpServletResponse response) {
- try {
- request.setCharacterEncoding("UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- response.setCharacterEncoding("UTF-8");
-
-
- String respMessage = coreService.processRequest(request);
-
-
- PrintWriter out = null;
- try {
- out = response.getWriter();
- out.print(respMessage);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- out.close();
- out = null;
- }
- }
-
- }
效果如下:
ok,大功告成,消息的接收和发送就写完了。
微信公众平台开发教程Java版(三) 消息接收和发送
标签:注意 ssr ring bool cep 教程 mat space 播放
原文地址:https://www.cnblogs.com/telwanggs/p/11905008.html