微信公众平台java开发详解(工程代码+解析)
说明:
本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让初学者尽快上手。
在阅读本文之前,应对微信公众平台的官方开发文档有所了解,知道接收和发送的都是xml格式的数据。另外,在做内容回复时用到了捷微JEEWX,这是一个自然语言解析的开放平台,可以帮我们解决整个微信开发过程中最困难的问题,此处不多讲,下面会有其详细的调用方式。
1.1 在登录微信官方平台之后,开启开发者模式,此时需要我们填写url和token,所谓url就是我们自己服务器的接口,用WechatServlet.java来实现,相关解释已经在注释中说明,代码如下:
- packagedemo.servlet;
- importjava.io.BufferedReader;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.io.InputStreamReader;
- importjava.io.OutputStream;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importdemo.process.WechatProcess;
- /**
- * 微信服务端收发消息接口
- *
- * @author pamchen-1
- *
- */
- publicclassWechatServletextendsHttpServlet {
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request
- * the request send by the client to the server
- * @param response
- * the response send by the server to the client
- * @throws ServletException
- * if an error occurred
- * @throws IOException
- * if an error occurred
- */
- publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)
- throwsServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- response.setCharacterEncoding("UTF-8");
- /** 读取接收到的xml消息 */
- StringBuffer sb = newStringBuffer();
- InputStream is = request.getInputStream();
- InputStreamReader isr = newInputStreamReader(is,"UTF-8");
- BufferedReader br = newBufferedReader(isr);
- String s = "";
- while((s = br.readLine()) !=null) {
- sb.append(s);
- }
- String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据
- String result = "";
- /** 判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回 */
- String echostr = request.getParameter("echostr");
- if(echostr !=null&& echostr.length() >1) {
- result = echostr;
- } else{
- //正常的微信处理流程
- result = newWechatProcess().processWechatMag(xml);
- }
- try{
- OutputStream os = response.getOutputStream();
- os.write(result.getBytes("UTF-8"));
- os.flush();
- os.close();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * The doPost method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to
- * post.
- *
- * @param request
- * the request send by the client to the server
- * @param response
- * the response send by the server to the client
- * @throws ServletException
- * if an error occurred
- * @throws IOException
- * if an error occurred
- */
- publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)
- throwsServletException, IOException {
- doGet(request, response);
- }
- }
1.2 相应的web.xml配置信息如下,在生成WechatServlet.java的同时,可自动生成web.xml中的配置。前面所提到的url处可以填写例如:http;//服务器地址/项目名/wechat.do
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appversion="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <description>This is the description of my J2EE component</description>
- <display-name>This is the display name of my J2EE component</display-name>
- <servlet-name>WechatServlet</servlet-name>
- <servlet-class>demo.servlet.WechatServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>WechatServlet</servlet-name>
- <url-pattern>/wechat.do</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
1.3 通过以上代码,我们已经实现了微信公众平台开发的框架,即开通开发者模式并成功接入、接收消息和发送消息这三个步骤。
下面就讲解其核心部分——解析接收到的xml数据,并以文本类消息为例,通过图灵机器人api接口实现智能回复。
2.1 首先看一下整体流程处理代码,包括:xml数据处理、调用图灵api、封装返回的xml数据。
- packagedemo.process;
- importjava.util.Date;
- importdemo.entity.ReceiveXmlEntity;
- /**
- * 微信xml消息处理流程逻辑类
- * @author pamchen-1
- *
- */
- publicclassWechatProcess {
- /**
- * 解析处理xml、获取智能回复结果(通过图灵机器人api接口)
- * @param xml 接收到的微信数据
- * @return 最终的解析结果(xml格式数据)
- */
- publicString processWechatMag(String xml){
- /** 解析xml数据 */
- ReceiveXmlEntity xmlEntity = newReceiveXmlProcess().getMsgEntity(xml);
- /** 以文本消息为例,调用图灵机器人api接口,获取回复内容 */
- String result = "";
- if("text".endsWith(xmlEntity.getMsgType())){
- result = newTulingApiProcess().getTulingResult(xmlEntity.getContent());
- }
- /** 此时,如果用户输入的是“你好”,在经过上面的过程之后,result为“你也好”类似的内容
- * 因为最终回复给微信的也是xml格式的数据,所有需要将其封装为文本类型返回消息
- * */
- result = newFormatXmlProcess().formatXmlAnswer(xmlEntity.getFromUserName(), xmlEntity.getToUserName(), result);
- returnresult;
- }
- }
2.2 解析接收到的xml数据,此处有两个类,ReceiveXmlEntity.java和ReceiveXmlProcess.java,通过反射的机制动态调用实体类中的set方法,可以避免很多重复的判断,提高代码效率,代码如下:
- packagedemo.entity;
- /**
- * 接收到的微信xml实体类
- * @author pamchen-1
- *
- */
- publicclassReceiveXmlEntity {
- privateString ToUserName="";
- privateString FromUserName="";
- privateString CreateTime="";
- privateString MsgType="";
- privateString MsgId="";
- privateString Event="";
- privateString EventKey="";
- privateString Ticket="";
- privateString Latitude="";
- privateString Longitude="";
- privateString Precision="";
- privateString PicUrl="";
- privateString MediaId="";
- privateString Title="";
- privateString Description="";
- privateString Url="";
- privateString Location_X="";
- privateString Location_Y="";
- privateString Scale="";
- privateString Label="";
- privateString Content="";
- privateString Format="";
- privateString Recognition="";
- publicString getRecognition() {
- returnRecognition;
- }
- publicvoidsetRecognition(String recognition) {
- Recognition = recognition;
- }
- publicString getFormat() {
- returnFormat;
- }
- publicvoidsetFormat(String format) {
- Format = format;
- }
- publicString getContent() {
- returnContent;
- }
- publicvoidsetContent(String content) {
- Content = content;
- }
- publicString getLocation_X() {
- returnLocation_X;
- }
- publicvoidsetLocation_X(String locationX) {
- Location_X = locationX;
- }
- publicString getLocation_Y() {
- returnLocation_Y;
- }
- publicvoidsetLocation_Y(String locationY) {
- Location_Y = locationY;
- }
- publicString getScale() {
- returnScale;
- }
- publicvoidsetScale(String scale) {
- Scale = scale;
- }
- publicString getLabel() {
- returnLabel;
- }
- publicvoidsetLabel(String label) {
- Label = label;
- }
- publicString getTitle() {
- returnTitle;
- }
- publicvoidsetTitle(String title) {
- Title = title;
- }
- publicString getDescription() {
- returnDescription;
- }
- publicvoidsetDescription(String description) {
- Description = description;
- }
- publicString getUrl() {
- returnUrl;
- }
- publicvoidsetUrl(String url) {
- Url = url;
- }
- publicString getPicUrl() {
- returnPicUrl;
- }
- publicvoidsetPicUrl(String picUrl) {
- PicUrl = picUrl;
- }
- publicString getMediaId() {
- returnMediaId;
- }
- publicvoidsetMediaId(String mediaId) {
- MediaId = mediaId;
- }
- publicString getEventKey() {
- returnEventKey;
- }
- publicvoidsetEventKey(String eventKey) {
- EventKey = eventKey;
- }
- publicString getTicket() {
- returnTicket;
- }
- publicvoidsetTicket(String ticket) {
- Ticket = ticket;
- }
- publicString getLatitude() {
- returnLatitude;
- }
- publicvoidsetLatitude(String latitude) {
- Latitude = latitude;
- }
- publicString getLongitude() {
- returnLongitude;
- }
- publicvoidsetLongitude(String longitude) {
- Longitude = longitude;
- }
- publicString getPrecision() {
- returnPrecision;
- }
- publicvoidsetPrecision(String precision) {
- Precision = precision;
- }
- publicString getEvent() {
- returnEvent;
- }
- publicvoidsetEvent(String event) {
- Event = event;
- }
- publicString getMsgId() {
- returnMsgId;
- }
- publicvoidsetMsgId(String msgId) {
- MsgId = msgId;
- }
- publicString getToUserName() {
- returnToUserName;
- }
- publicvoidsetToUserName(String toUserName) {
- ToUserName = toUserName;
- }
- publicString getFromUserName() {
- returnFromUserName;
- }
- publicvoidsetFromUserName(String fromUserName) {
- FromUserName = fromUserName;
- }
- publicString getCreateTime() {
- returnCreateTime;
- }
- publicvoidsetCreateTime(String createTime) {
- CreateTime = createTime;
- }
- publicString getMsgType() {
- returnMsgType;
- }
- publicvoidsetMsgType(String msgType) {
- MsgType = msgType;
- }
- }
- packagedemo.process;
- importjava.lang.reflect.Field;
- importjava.lang.reflect.Method;
- importjava.util.Iterator;
- importorg.dom4j.Document;
- importorg.dom4j.DocumentHelper;
- importorg.dom4j.Element;
- importdemo.entity.ReceiveXmlEntity;
- /**
- * 解析接收到的微信xml,返回消息对象
- * @author pamchen-1
- *
- */
- publicclassReceiveXmlProcess {
- /**
- * 解析微信xml消息
- * @param strXml
- * @return
- */
- publicReceiveXmlEntity getMsgEntity(String strXml){
- ReceiveXmlEntity msg = null;
- try{
- if(strXml.length() <=0|| strXml ==null)
- returnnull;
- // 将字符串转化为XML文档对象
- Document document = DocumentHelper.parseText(strXml);
- // 获得文档的根节点
- Element root = document.getRootElement();
- // 遍历根节点下所有子节点
- Iterator<?> iter = root.elementIterator();
- // 遍历所有结点
- msg = newReceiveXmlEntity();
- //利用反射机制,调用set方法
- //获取该实体的元类型
- Class<?> c = Class.forName("demo.entity.ReceiveXmlEntity");
- msg = (ReceiveXmlEntity)c.newInstance();//创建这个实体的对象
- while(iter.hasNext()){
- Element ele = (Element)iter.next();
- //获取set方法中的参数字段(实体类的属性)
- Field field = c.getDeclaredField(ele.getName());
- //获取set方法,field.getType())获取它的参数数据类型
- Method method = c.getDeclaredMethod("set"+ele.getName(), field.getType());
- //调用set方法
- method.invoke(msg, ele.getText());
- }
- } catch(Exception e) {
- // TODO: handle exception
- System.out.println("xml 格式异常: "+ strXml);
- e.printStackTrace();
- }
- returnmsg;
- }
- }
2.3调用捷微JEEWXapi接口,获取智能回复内容:
- packagedemo.process;
- importjava.io.IOException;
- importjava.io.UnsupportedEncodingException;
- importjava.net.URLEncoder;
- importorg.apache.http.HttpResponse;
- importorg.apache.http.client.ClientProtocolException;
- importorg.apache.http.client.methods.HttpGet;
- importorg.apache.http.impl.client.HttpClients;
- importorg.apache.http.util.EntityUtils;
- importorg.json.JSONException;
- importorg.json.JSONObject;
- /**
- * 调用图灵机器人api接口,获取智能回复内容
- * @author pamchen-1
- *
- */
- publicclassTulingApiProcess {
- /**
- * 调用图灵机器人api接口,获取智能回复内容,解析获取自己所需结果
- * @param content
- * @return
- */
- publicString getTulingResult(String content){
- /** 此处为图灵api接口,参数key需要自己去注册申请,先以11111111代替 */
- String apiUrl = "http://www.tuling123.com/openapi/api?key=11111111&info=";
- String param = "";
- try{
- param = apiUrl+URLEncoder.encode(content,"utf-8");
- } catch(UnsupportedEncodingException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } //将参数转为url编码
- /** 发送httpget请求 */
- HttpGet request = newHttpGet(param);
- String result = "";
- try{
- HttpResponse response = HttpClients.createDefault().execute(request);
- if(response.getStatusLine().getStatusCode()==200){
- result = EntityUtils.toString(response.getEntity());
- }
- } catch(ClientProtocolException e) {
- e.printStackTrace();
- } catch(IOException e) {
- e.printStackTrace();
- }
- /** 请求失败处理 */
- if(null==result){
- return"对不起,你说的话真是太高深了……";
- }
- try{
- JSONObject json = newJSONObject(result);
- //以code=100000为例,参考图灵机器人api文档
- if(100000==json.getInt("code")){
- result = json.getString("text");
- }
- } catch(JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- returnresult;
- }
- }
2.4 将结果封装为微信规定的xml格式,并返回给1.1中创建的servlet接口。
- packagedemo.process;
- importjava.util.Date;
- /**
- * 封装最终的xml格式结果
- * @author pamchen-1
- *
- */
- publicclassFormatXmlProcess {
- /**
- * 封装文字类的返回消息
- * @param to
- * @param from
- * @param content
- * @return
- */
- publicString formatXmlAnswer(String to, String from, String content) {
- StringBuffer sb = newStringBuffer();
- Date date = newDate();
- sb.append("<xml><ToUserName><![CDATA[");
- sb.append(to);
- sb.append("]]></ToUserName><FromUserName><![CDATA[");
- sb.append(from);
- sb.append("]]></FromUserName><CreateTime>");
- sb.append(date.getTime());
- sb.append("</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[");
- sb.append(content);
- sb.append("]]></Content><FuncFlag>0</FuncFlag></xml>");
- returnsb.toString();
- }
- }
总结,以上便是微信公众平台开发的全部流程,整体来看并不复杂,要非常感谢捷微JEEWX,帮我们解决了智能回复这一高难度问题。其他类型的消息处理与示例中类似,有兴趣的开发者可以联系我进行交流学习,希望本文对大家有所帮助。
微信公众平台java开发详解(工程代码+解析)相关推荐
- 微信公众平台java开发详解
说明: 本次的教程主要是对微信公众平台开发者模式的讲解,网络上很多类似文章,但很多都让初学微信开发的人一头雾水,所以总结自己的微信开发经验,将微信开发的整个过程系统的列出,并对主要代码进行讲解分析,让 ...
- 微信公众平台认证步骤详解及服务号和订阅号区别---之微信开发一
微信公众号对象: 企业.媒体.以及公益.社区等组织.机构. 微信公众号作用: 通过微信公众渠道将品牌推广给上亿或者更多的微信用户. 1.极快的传播速度.极少的宣传成本: 2.提高品牌知名度,打造更具影 ...
- 微信公众平台java开发
准备工作 建立web工程,搭建服务器. ---- 这里使用springBoot搭建 注册微信公众号(开发时用测试号即可). 使用内网穿透软件(ngrok),使外网能访问你本地的应用,省去部署. 微信公 ...
- 微信公众平台Java开发如何让网页自适应不同分辨率的手机浏
在做微信公众平台开发的朋友应该会遇到这样的问题,开发的网页在不同分辨率的手机显示大小不同,不能根据分辨率的不同而自动调节大小,下面就告诉大家如何解决. 1. 使用HTML中的viewpo ...
- 微信公众平台认证步骤详解及服务号和订阅号区别
微信公众号对象: 企业.媒体.以及公益.社区等组织.机构. 微信公众号作用: 通过微信公众渠道将品牌推广给上亿或者更多的微信用户. 1.极快的传播速度.极少的宣传成本: 2.提高品牌知名度,打造更具影 ...
- 微信公众平台认证步骤详解及服务号和订阅号区别 分类: 微信 2014
微信公众号对象: 企业.媒体.以及公益.社区等组织.机构. 微信公众号作用: 通过微信公众渠道将品牌推广给上亿或者更多的微信用户. 1.极快的传播速度.极少的宣传成本: 2.提高品牌知名度,打造更具影 ...
- 微信公众平台java开发之接口url与token填写
接口url与token填写注意的地方:你填写的请求的url需要包含token,这样才能让微信服务器与自己的服务器进行token验证 项目里面的token设定要和微信公共平台里面填写的那个token要保 ...
- 微信公众开放平台开发03---百度BAE上搭建属于自己的微信公众平台 -JAVA,微信公众开放平台部署到百度云中BASE2.0,进行调试,木有钱买云服务器的亲们试试
微信公众开放平台开发03---百度BAE上搭建属于自己的微信公众平台 -JAVA,微信公众开放平台部署到百度云中BASE2.0,进行调试,木有钱买云服务器的亲们试试 技术qq交流群:JavaDream ...
- 百度云搭建微信公众平台服务器,微信大众开放平台开发03-百度BAE上搭建属于自己的微信公众平台 -JAVA,微信公众开放平台部署到百度云中BASE2.0,进行调试,木有钱买云服务器的亲们试试...
微信公众开放平台开发03---百度BAE上搭建属于自己的微信公众平台 -JAVA,微信公众开放平台部署到百度云中BASE2.0,进行调试,木有钱买云服务器的亲们试试 微信公众开放平台开发03---百度 ...
最新文章
- MIT与商汤科技成立人工智能联盟
- java script 技巧
- Android知识点小结
- ZBrushCore中文版
- mysql复制文件迁移后看不到表_mysql 直接拷贝data 目录下文件 进行数据库迁移时遇到的一些问题??...
- Python实训day06am【网络爬虫(爬取接口)】
- Solr所有的查询解析器Query Parsers(转:http://blog.csdn.net/jiangchao858/article/details/53859731)
- 什么是套接字?Socket基本介绍
- 95-136-043-源码-Operator-CoProcessOperator
- Overloud TH3 for Mac - 电吉他效果器
- 感知层在物联网中的重要性
- [论文阅读] (19)英文论文Evaluation(实验数据集、指标和环境)如何描述及精句摘抄——以系统AI安全顶会为例
- IDA Pro 权威指南学习笔记(十三) - 基本代码转换
- Python12306自动抢票下单,五一旅游回家就选Python
- Ralink SDK相关指令总结
- 计算机word考试试题模板,2017年职称计算机考试Word2003巩固练习题13
- CSR867x 之充电配置开发
- 自控重点整理1.1 比例微分PD控制器的作用
- 纪念我的纪念--转正申请
- .NET 程序员有家了,微软推出官方技术社区论坛