
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; }
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年03月15日 03时26分37秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MATLAB在线编辑网站及使用教程
2019-03-04
IOC容器_Bean管理xml方式
2019-03-04
python+Aritest自动化—02—app_util.py—app驱动
2019-03-04
Typora的基础用法
2019-03-04
蓝桥杯入门练习题斐波那契数列
2019-03-04
Linux-find
2019-03-04
后台守护线程
2019-03-04
volatile关键字
2019-03-04
(JAVA常用类库)CharSequence接口
2019-03-04
(Java基础类库 )System类
2019-03-04
context:include-filter与exclude-filte控制扫描组件
2019-03-04
Two Day今日程序学习记录->关于指针的一点问题以及16进制转10进制
2019-03-04
《Java---------java环境搭建》
2019-03-04
【SSL】1072砝码称重
2019-03-04
【SSL】2294打包
2019-03-04
标程_高精度运算
2019-03-04
js数据结构--队列--常见操作
2019-03-04
JS数据结构--单向链表--常见操作
2019-03-04
【SSL】1606&【洛谷】P2014选课
2019-03-04
JS数据结构--双向链表--常见操作
2019-03-04