当前位置: 移动技术网 > IT编程>开发语言>Java > Java微信公众平台开发(2) 微信服务器post消息体的接收

Java微信公众平台开发(2) 微信服务器post消息体的接收

2019年07月22日  | 移动技术网IT编程  | 我要评论

在上一篇的文章中我们详细讲述了如何将我们的应用服务器和微信腾讯服务器之间的对接操作,最后接入成功,不知道你有没有发现在上一篇的【controller】中我定义了一个get方法和一个post方法,但是在使用过程中我们就用了get方法,这里我们就来说说我们预留的post的方法的使用!

当我们在完成了服务器验证之后,此后用户每次向公众号发送消息、或者产生自定义菜单点击事件时,开发者填写的服务器配置url将得到微信服务器推送过来的消息和事件,然后开发者可以依据自身业务逻辑进行响应,例如回复消息等!通过这句话我们能知道后面所有的微信服务器和我们应用服务器之间的沟通都是通过post消息体来完成的,那么我们这里将讲述如何接受微信post的消息体!

(一)消息类型和消息格式

上面有说道我们所有的和微信服务器之间进行沟通基本都是通过post消息体完成的,首先我们了解下消息体的类型,大致类型有两种:

普通消息类型:文本消息、图片消息、语音消息、视频消息、小视频消息、地理位置消息、链接消息

事件消息类型:关注/取消关注事件、扫描带参数二维码事件、上报地理位置事件、自定义菜单事件、点击菜单拉取消息时的事件推送、点击菜单跳转链接时的事件推送
消息类型:微信服务端推送的所有消息体的类型格式都是xml格式;

(二)消息重试机制

微信服务器在五秒内收不到响应会断掉连接,并且重新发起请求,总共重试三次。假如服务器无法保证在五秒内处理并回复,可以直接回复空串,微信服务器不会对此作任何处理,并且不会发起重试,但是这里后期可以使用【客服消息接口】去完成消息再次推送。

(三)消息接收处理

在前面我们有说道微信的消息体是采用xml格式,那么我在这里写了一个messageutil去做消息格式的处理,大致代码如下:

package com.cuiyongzhi.wechat.util;
import java.io.inputstream;
import java.io.writer;
import java.util.hashmap;
import java.util.list;
import java.util.map;
 
import javax.servlet.http.httpservletrequest;
 
import org.dom4j.document;
import org.dom4j.element;
import org.dom4j.io.saxreader;
 
import com.thoughtworks.xstream.xstream;
import com.thoughtworks.xstream.core.util.quickwriter;
import com.thoughtworks.xstream.io.hierarchicalstreamwriter;
import com.thoughtworks.xstream.io.xml.prettyprintwriter;
import com.thoughtworks.xstream.io.xml.xppdriver;
  
/**
 * classname: messageutil
 * @description: 消息工具类
 * @author dapengniao
 * @date 2016年3月7日 上午10:05:04
 */
public class messageutil { 
  
  /** 
   * 返回消息类型:文本 
   */ 
  public static final string resp_message_type_text = "text"; 
  
  /** 
   * 返回消息类型:音乐 
   */ 
  public static final string resp_message_type_music = "music"; 
  
  /** 
   * 返回消息类型:图文 
   */ 
  public static final string resp_message_type_news = "news"; 
  
  /** 
   * 请求消息类型:文本 
   */ 
  public static final string req_message_type_text = "text"; 
  
  /** 
   * 请求消息类型:图片 
   */ 
  public static final string req_message_type_image = "image"; 
  
  /** 
   * 请求消息类型:链接 
   */ 
  public static final string req_message_type_link = "link"; 
  
  /** 
   * 请求消息类型:地理位置 
   */ 
  public static final string req_message_type_location = "location"; 
  
  /** 
   * 请求消息类型:音频 
   */ 
  public static final string req_message_type_voice = "voice"; 
  
  /** 
   * 请求消息类型:推送 
   */ 
  public static final string req_message_type_event = "event"; 
  
  /** 
   * 事件类型:subscribe(订阅) 
   */ 
  public static final string event_type_subscribe = "subscribe"; 
  
  /** 
   * 事件类型:unsubscribe(取消订阅) 
   */ 
  public static final string event_type_unsubscribe = "unsubscribe"; 
  
  /** 
   * 事件类型:click(自定义菜单点击事件) 
   */ 
  public static final string event_type_click = "click"; 
  
  /**
   * @description: 解析微信发来的请求(xml) 
   * @param @param request
   * @param @return
   * @param @throws exception  
   * @author dapengniao
   * @date 2016年3月7日 上午10:04:02
   */
  @suppresswarnings("unchecked")
  public static map<string, string> parsexml(httpservletrequest request) throws exception { 
    // 将解析结果存储在hashmap中  
    map<string, string> map = new hashmap<string, string>(); 
  
    // 从request中取得输入流  
    inputstream inputstream = request.getinputstream(); 
    // 读取输入流  
    saxreader reader = new saxreader(); 
    document document = reader.read(inputstream); 
    // 得到xml根元素  
    element root = document.getrootelement(); 
    // 得到根元素的所有子节点  
    list<element> elementlist = root.elements(); 
  
    // 遍历所有子节点  
    for (element e : elementlist) 
      map.put(e.getname(), e.gettext()); 
  
    // 释放资源  
    inputstream.close(); 
    inputstream = null; 
  
    return map; 
  } 
  
  @suppresswarnings("unused")
  private static xstream xstream = new xstream(new xppdriver() { 
    public hierarchicalstreamwriter createwriter(writer out) { 
      return new prettyprintwriter(out) { 
        // 对所有xml节点的转换都增加cdata标记  
        boolean cdata = true; 
        @suppresswarnings("rawtypes")
        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); 
          } 
        } 
      }; 
    } 
  }); 
}

在这个方法体里需要用到部分依赖,需要在pom文件加入如下部分:

<!-- xml -->
<dependency>
  <groupid>org.apache.directory.studio</groupid>
  <artifactid>org.dom4j.dom4j</artifactid>
  <version>1.6.1</version>
</dependency>
 
<dependency>
  <groupid>com.thoughtworks.xstream</groupid>
  <artifactid>xstream</artifactid>
  <version>1.4.8</version>
</dependency>

然后将我们的wechatsecurity controller中的post方法修改为如下,用于做消息的接收和处理:

@requestmapping(value = "security", method = requestmethod.post)
  // post方法用于接收微信服务端消息
  public void dopost(httpservletrequest request,httpservletresponse response) {
    system.out.println("这是post方法!");
    try{
    map<string, string> map=messageutil.parsexml(request);
    system.out.println("============================="+map.get("content"));
    }catch(exception e){
      logger.error(e,e);
    }
  }

因为前面我们已经开启了我们的开发者模式,那么当我们在这里将我们代码发布之后再公众号上发送消息,在们的后台就能看到我们的消息体进入并解析成功了,这里我输出的是微信的【原始id】,截图大致如下:

在这里我只是做了消息体的接收和转换成map,并没有对消息做出来,那么下一篇我们将讲述对消息的分类处理!感谢你的翻阅,如有疑问可以留言讨论!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网