JAVA微信开发weixin4j新手接入之Servlet方式接入
来源:互联网 发布:接电话变声软件 编辑:程序博客网 时间:2024/04/28 05:21
今天为大家来演示一下,使用Java Servlet方式,用weixin4j如何进行新手接入。
首先大家需要新建一个空的web项目。
下面是代码:
package org.weixin4j.demo; import com.thoughtworks.xstream.XStream;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.weixin4j.Configuration;import org.weixin4j.message.OutputMessage;import org.weixin4j.message.TextOutputMessage;import org.weixin4j.util.TokenUtil;import org.weixin4j.util.XStreamFactory; /** * 新手接入 * 本次接入设定的访问URL为:http://www.weixin4j.org/api/公众号名称 * @author weixin4j<weixin4j@ansitech.com> */public class WechatServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getServletPath(); //1.验证消息真实性 //获取微信公众号 String gongzonghao = path.substring(path.lastIndexOf("/")); //如果获取不到公众号,则向服务器发生错误信息 if (gongzonghao == null) { response.getWriter().write("error"); } else { //根据公众号,算出对应的Token,然后进行验证 gongzonghao = gongzonghao.substring(1); //获取配置文件配置的Token String token = Configuration.getProperty("weixin4j.token"); if (Configuration.isDebug()) { System.out.println("取出公众号Token:" + token); } //成为开发者验证 String signature = request.getParameter("signature"); // 微信加密签名 String timestamp = request.getParameter("timestamp"); // 时间戳 String nonce = request.getParameter("nonce"); // 随机数 String echostr = request.getParameter("echostr"); // //确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败 if (TokenUtil.checkSignature(token, signature, timestamp, nonce)) { response.getWriter().write(echostr); } else { response.getWriter().write("error"); } } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OutputMessage outputMsg = new TextOutputMessage("您的消息服务器已收到!"); // 把发送发送对象转换为xml输出 XStream xs = XStreamFactory.init(true); xs.alias("xml", outputMsg.getClass()); String xml = xs.toXML(outputMsg); if (Configuration.isDebug()) { System.out.println("POST输出消息:[" + xml + "]"); } response.getWriter().write(xml); }}接下来是配置web.xml:
<!-- Servlet方式 微信接入配置 --><servlet> <servlet-name>wechat</servlet-name> <servlet-class>org.weixin4j.demo.WechatServlet</servlet-class></servlet><servlet-mapping> <servlet-name>wechat</servlet-name> <url-pattern>/api/*</url-pattern></servlet-mapping>
最后,将项目打包,发布到服务器,就可以在微信公众号平台中,进行开发者接入认证了。
祝你部署成功,生活愉快!
明天会有更精彩的内容,别走开,马上回来!
您可以加入我们的官方QQ群:365736353,专业人为您解答各种技术问题。
转载至:http://www.weixin4j.org/article/weixin4j-servlet.html更多文章,请关注:http://www.weixin4j.org/
1 0
- JAVA微信开发weixin4j新手接入之Servlet方式接入
- JAVA微信开发weixin4j新手接入之Servlet方式接入
- weixin4j微信开发-零开发新手接入
- weixin4j微信开发-零开发新手接入
- JAVA微信开发-新手接入指南
- JAVA微信开发-新手接入指南
- JAVA微信开发-新手接入指南
- JAVA微信开发-新手接入指南
- JAVA微信开发-新手接入指南
- JAVA微信开发-新手接入指南
- Java微信开发之接入验证
- 手把手教您开发JAVA微信SDK-新手接入
- JAVA微信开发之weixin4j入门视频
- JAVA微信开发之weixin4j入门视频
- 微信开发接入
- 微信开发-接入
- Java微信公众平台开发之接入
- 【Servlet】微信接入算法
- 解决Unity中,使用Ngui 的默认字体Arial,在部分Android设备显示中文不清楚,或者显示不出来问题
- 【流媒體】jrtplib—VS2010下RTP开源协议库JRTPLIB3.9.1编译
- 如何给虚拟机上的CentOS添加一块硬盘呢?
- JAVA分代垃圾回收机制
- Ubuntu-14.10添加root登录
- JAVA微信开发weixin4j新手接入之Servlet方式接入
- Movie Store OpenCart 自适应主题模板 ABC-0249
- Java面向对象
- webview使用小注意
- Hinton关于RBM的代码注解之(三)mnistclassify.m
- ThreadPoolExecutor的应用和实现分析(续)—— 任务饱和丢弃策略
- 【memcache】客户端 cmd操作常用指令
- 类和对象的思维导图
- [andorid]_[初级]_[修改手机通讯录联系人信息-联系人名称、手机号码、电子邮件、即时信息、地址、组织]]