• 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";
            }
        }  
    
    
  • 相关阅读:
    全局获取Context
    下拉刷新 SwipeRefreshLayout
    更高级的ToolBar使用----AppBarLayout
    卡片布局CardView
    Design Support的CoordinatorLayout布局
    悬浮按钮FloatingActionButton
    继续滑动菜单的完善,NavigationView,图片圆形化-CircleImageView
    修改电脑hosts文件
    Activity生命周期和启动模式
    活动的基本用法
  • 原文地址:https://www.cnblogs.com/qixidi/p/10221977.html
Copyright © 2020-2023  润新知