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";
        }
    }  

posted @ 2019-01-04 19:20  栖息地  阅读(149)  评论(0编辑  收藏  举报