Spring Boot -- 微信企业号开发02-启用企业应用的消息服务器

来源:互联网 发布:淘宝模特一天多少钱 编辑:程序博客网 时间:2024/05/17 06:53

微信企业号开发

创建项目

以Spring Boot开发微信企业号.

  1. pom.xml依赖
    添加依赖:web,webservice,数据库的mysql与jpa.还需要额外添加xstream与commons-codec
    xstream是xml的解析框架,用于对微信消息的收发的转化.
    commons-codec是加密解密框架,用于微信连接时加密与解密

    <!-- 加密解密 --><dependency>    <groupId>commons-codec</groupId>    <artifactId>commons-codec</artifactId></dependency><!-- xml解析 --><dependency>    <groupId>com.thoughtworks.xstream</groupId>    <artifactId>xstream</artifactId>    <version>1.4.10</version></dependency>

  2. 下载微信提供的加密解密库
    下载地址:https://work.weixin.qq.com/api/doc#10128
    这里写图片描述
    将com复制到项目.
    在lib中,导入了commons-codec.jar,在第一步已经通过maven导入

设置API接收

    以微信自建应用-企业小助手为例.    企业小助手 --> 接受消息 --> 设置API接收    ![这里写图片描述](http://img.blog.csdn.net/20170914105833076?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZDI5MjIyMjEwMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)生成token与encodingAESKey

编写接口

创建Controller类    import org.springframework.web.bind.annotation.GetMapping;    import org.springframework.web.bind.annotation.RequestMapping;    import org.springframework.web.bind.annotation.RequestParam;    import org.springframework.web.bind.annotation.RestController;    import com.qq.weixin.mp.aes.WXBizMsgCrypt;    /**     * 企业小助手     */    @RestController    @RequestMapping("/api/assistant")    public class AssistantController {        String token = "yucoang";        String encodingAESKey = "3z1VY4h47s1fFO2IAcLSYiXDomc0JQug8llOwqqCwHt";        String corpID = "wx5c28d34505a16979";        /**         * 初始token认证         * @return         */        @GetMapping        public String get(                @RequestParam String msg_signature,                @RequestParam String timestamp,                 @RequestParam String nonce,                @RequestParam String echostr){            String sEchoStr = null; // 需要返回的明文            try {                WXBizMsgCrypt wxcpt;                wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpID);                sEchoStr = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);            } catch (Exception e) {                e.printStackTrace();            }            return sEchoStr;        }           /**         * 接收的消息,事件         */        @PostMapping        public void post(                @RequestParam String msg_signature,                @RequestParam String timestamp,                @RequestParam String nonce,                @RequestBody String text) throws Exception{            WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey, corpID);            String data = wxcpt.DecryptMsg(msg_signature, timestamp, nonce, text);            System.out.println(data);        }    }

开启花生壳,启动项目.

将花生壳的域名填入消息服务器的URL地址.
开启花生壳将域名映射为本地的8080端口.
运行项目后,设置API接收的页面点击保存

这里写图片描述

    2017-09-14 12:06:54.200  INFO 7148 --- [           main] c.y.e.QiotWechatEnterpriseApplication    : Started QiotWechatEnterpriseApplication in 8.272 seconds (JVM running for 9.379)    <xml><ToUserName><![CDATA[wx5c28d34505a16979]]></ToUserName><FromUserName><![CDATA[yucoang]]></FromUserName><CreateTime>1505361869</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[11]]></Content><MsgId>1506621069</MsgId><AgentID>0</AgentID></xml>
阅读全文
0 0
原创粉丝点击