https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html,
收到的关注事件或者取消关注事件消息体是空的,导致接收的服务报了下面的错误
org.dom4j.DocumentException: Error on line 1 of document : Premature end of file. Nested exception:
接收关注/取消关注事件的代码:
public static Map<String, String> parseXml(HttpServletRequest request, WeChatConfig config) throws Exception {
// 将解析结果存储在HashMap中
Map<String, String> map = new HashMap<String, String>();
log.info("###########################################");
// 从request中取得输入流
try(InputStreamReader inputStreamReader = new InputStreamReader(request.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)){
StringBuilder stringBuilder = new StringBuilder();
String line;
while((line = bufferedReader.readLine()) != null){
stringBuilder.append(line);
}
log.info("from微信:{}, length:{}", stringBuilder, stringBuilder.length());
}
try(InputStream inputStream = request.getInputStream()) {
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
String xmlBody = document.asXML();
log.info("xmlBody:{}", xmlBody);
if(StringUtils.isEmpty(xmlBody.trim())){
throw new ThirdException("请求数据为空!");
}
if(StringUtils.isNotEmpty(xmlBody) && xmlBody.contains("Encrypt")){
//解密请求消息体
String nXmlString = decryptMsg(request, xmlBody,config);
document = DocumentHelper.parseText(nXmlString);
}
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
@SuppressWarnings("unchecked")
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
}
return map;
}
代码 Document document = reader.read(inputStream); 处报了上面的错误,from微信打印了8个空白字符串
这个有进展吗?遇到同样的问题了
你好,是事件通知吗