springmvc 整合微信


springmvc 整合微信


  
方式一:
 ①  配置验证
   @RequestMapping(value = "/into", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public void validate(WeChat wc, PrintWriter out) {
        String signature = wc.getSignature(); // 微信加密签名
        String timestamp = wc.getTimestamp(); // 时间戳
        String nonce = wc.getNonce();// 随机数
        String echostr = wc.getEchostr();// 随机字符串
        System.out.println("加密的签名字符串:" + signature);
        System.out.println("时间戳:" + timestamp);
        System.out.println("随机数:" + nonce);
        System.out.println("随机字符串:" + echostr); // 验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
        if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
            // 随机字符串
            System.out.println("我进来了");
            out.print(echostr);
        } else {
            System.out.println("不是微信服务器发来的请求,请小心!");
        }
        out.flush();
        out.close();
    }
② 数据处理
    @RequestMapping(value = "/into", method = RequestMethod.POST, produces = "application/xml;charset=UTF-8")
    public void dispose(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        // 调用核心业务类接收消息、处理消息
        // 从请求中读取整个post数据
        // 将请求、响应的编码均设置为UTF-8(防止中文乱码)
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        InputStream inputStream = req.getInputStream();
        String datapacket = IOUtils.toString(inputStream, "UTF-8");
        String respMessage = CoreService.processRequest(datapacket);
        log.info(respMessage);
        // 响应消息
        out.print(respMessage);
        out.close();
    }

方式二:
@RequestMapping(value = "/into", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String validate(WeChat wc) {
        String signature = wc.getSignature(); // 微信加密签名
        String timestamp = wc.getTimestamp(); // 时间戳
        String nonce = wc.getNonce();// 随机数
        String echostr = wc.getEchostr();// 随机字符串
        System.out.println("加密的签名字符串:" + signature);
        System.out.println("时间戳:" + timestamp);
        System.out.println("随机数:" + nonce);
        System.out.println("随机字符串:" + echostr); // 验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
        if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
            // 随机字符串
            System.out.println("我进来了");
            return echostr;
        } else {
            System.out.println("不是微信服务器发来的请求,请小心!");
            return "error";
        }
    }
    @RequestMapping(value = "/into", method = RequestMethod.POST, produces = "application/xml;charset=UTF-8")
    public void dispose(HttpServletRequest req, WeChat wc,
            HttpServletResponse resp) throws IOException {
        String signature = wc.getSignature(); // 微信加密签名
        String timestamp = wc.getTimestamp(); // 时间戳
        String nonce = wc.getNonce();// 随机数
        String echostr = wc.getEchostr();// 随机字符串
        System.out.println("加密的签名字符串:" + signature);
        System.out.println("时间戳:" + timestamp);
        System.out.println("随机数:" + nonce);
        System.out.println("随机字符串:" + echostr); // 验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
            // 调用核心业务类接收消息、处理消息
            // 从请求中读取整个post数据
            InputStream inputStream = req.getInputStream();
            String datapacket = IOUtils.toString(inputStream, "UTF-8");
            String respMessage = CoreService.processRequest(datapacket);
            log.info(respMessage);
            // 响应消息
            out.print(respMessage);
        }
        out.flush();
        out.close();
    }

方式三:(推荐使用)
@RequestMapping(value = "/into", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String validate(WeChat wc) {
        String signature = wc.getSignature(); // 微信加密签名
        String timestamp = wc.getTimestamp(); // 时间戳
        String nonce = wc.getNonce();// 随机数
        String echostr = wc.getEchostr();// 随机字符串
        if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
            // 随机字符串
            log.info("我进来了");
            return echostr;
        } else {
            log.error("不是微信服务器发来的请求,请小心!");
            return "error";
        }
    }
    @RequestMapping(value = "/into", method = RequestMethod.POST, produces = "application/xml;charset=UTF-8")
    @ResponseBody
    public String dispose(HttpServletRequest req, WeChat wc) throws IOException {
        String signature = wc.getSignature(); // 微信加密签名
        String timestamp = wc.getTimestamp(); // 时间戳
        String nonce = wc.getNonce();// 随机数
        if (ValidationUtil.checkSignauer(signature, timestamp, nonce)) {
            // 调用核心业务类接收消息、处理消息
            // 从请求中读取整个post数据
            InputStream inputStream = req.getInputStream();
            String datapacket = IOUtils.toString(inputStream, "UTF-8");
            String respMessage = CoreService.processRequest(datapacket);
            log.info(respMessage);
            // 响应消息
            return respMessage;
        }else{
            log.error("数据处理失败!");
            return "error";
        }
    }  

转载于:https://www.cnblogs.com/qixidi/p/10221977.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值