• 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";
            }
        }  
    
    
  • 相关阅读:
    安装包报错2503解决方法
    js中return;、return true、return false;区别
    图片添加border 不占用图片的大小
    效果网址http://sc.chinaz.com/tag_jiaoben/tupianlunbo.html
    兼容ie8 rgba()用法
    html使用css让文字多行超出部分用省略号三个点显示的方法案例
    MyCat02--MyCat入门无ZK实践
    MyCat01--基础
    MySQL/MariaDB导入(load data infile)导出(select into outfile)
    MariaDB/Mysql临时表
  • 原文地址:https://www.cnblogs.com/qixidi/p/10221977.html
Copyright © 2020-2023  润新知