Spring获取公众号xml数据的两种方式
发布日期:2021-05-06 19:50:31 浏览次数:22 分类:原创文章

本文共 4231 字,大约阅读时间需要 14 分钟。

方式1:HttpServletRequest req


    @PostMapping(produces = "application/xml; charset=UTF-8")    public String post(HTTPServletRequest req,                       @RequestParam("signature") String signature,                       @RequestParam("timestamp") String timestamp,                       @RequestParam("nonce") String nonce,                       @RequestParam("openid") String openid,                       @RequestParam(name = "encrypt_type", required = false) String encType,                       @RequestParam(name = "msg_signature", required = false) String msgSignature) {        logger.info("notifyData"+requestData,requestData.toString());        logger.info("post:{},{},{},{},{}",signature,timestamp,nonce,openid,encType,msgSignature);        if (!CheckUtil.checkSignature(signature, timestamp, nonce)) {            throw new IllegalArgumentException("非法请求!");        }        Map<String, String> map = null;        try {          map = MessageUtil.xmlToMap(req);            wxAccountService.router(map,openid);            System.out.println(openid);        } catch (Exception e) {            e.printStackTrace();        }    return  "";    }

方式2:@RequestBody


requestData任意命名,即可把body中的xml数据转换为xml字符串。


    @PostMapping(produces = "application/xml; charset=UTF-8")    public String post(@RequestBody String requestData,                       @RequestParam("signature") String signature,                       @RequestParam("timestamp") String timestamp,                       @RequestParam("nonce") String nonce,                       @RequestParam("openid") String openid,                       @RequestParam(name = "encrypt_type", required = false) String encType,                       @RequestParam(name = "msg_signature", required = false) String msgSignature) {        logger.info("notifyData"+requestData,requestData.toString());        logger.info("post:{},{},{},{},{}",signature,timestamp,nonce,openid,encType,msgSignature);        if (!CheckUtil.checkSignature(signature, timestamp, nonce)) {            throw new IllegalArgumentException("非法请求!");        }        Map<String, String> map = null;        try {            map = WXPayUtil.xmlToMap(requestData);//          map = MessageUtil.xmlToMap(req);            wxAccountService.router(map,openid);            System.out.println(openid);        } catch (Exception e) {            e.printStackTrace();        }    return  "";    }

附赠转换工具


    public static Map<String, String> xmlToMap(String strXML) throws Exception {        try {            Map<String, String> data = new HashMap<String, String>();            DocumentBuilder documentBuilder = WXPayXmlUtil.newDocumentBuilder();            InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));            org.w3c.dom.Document doc = documentBuilder.parse(stream);            doc.getDocumentElement().normalize();            NodeList nodeList = doc.getDocumentElement().getChildNodes();            for (int idx = 0; idx < nodeList.getLength(); ++idx) {                Node node = nodeList.item(idx);                if (node.getNodeType() == Node.ELEMENT_NODE) {                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;                    data.put(element.getNodeName(), element.getTextContent());                }            }            try {                stream.close();            } catch (Exception ex) {                // do nothing            }            return data;        } catch (Exception ex) {            WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);            throw ex;        }    }

     * 利用dom4j读取xml文件存放在map对象中     */    public static Map<String,String> xmlToMap(HttpServletRequest request) throws Exception{        Map<String,String> map = new HashMap<String, String>();        SAXReader reader = new SAXReader();        InputStream ins = request.getInputStream();        Document doc = reader.read(ins);        Element root = doc.getRootElement();        List<Element> list = root.elements();        for(Element e:list){            map.put(e.getName(),e.getText());        }        ins.close();        System.out.println(map.toString()   );        return map;    }
上一篇:uni-rate子组件星星不更新的解决
下一篇:uni-app实现微信APP支付的全过程详解(附赠全部代码)

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年03月15日 03时26分37秒