美文网首页
request读流,判断不同的请求

request读流,判断不同的请求

作者: 武小寺 | 来源:发表于2017-05-13 12:35 被阅读20次

    xml

    // 取得输入流的xml
            Document doc = getContentXml(request.getInputStream());
            if (doc == null) {
                return setRtnMsg(400,"IO Error");
            }
            Element root = doc.getRootElement();
    //      HashMap<String, String> headElements = new HashMap<>();
            LinkedHashMap<String, String> headElements = new LinkedHashMap<>();
            // 取得xml的Head节点所有项目的名和值
            listHeadNodes(root, headElements);
            for (String key : headElements.keySet()) {
                // 不检查非必填项
                if ("service_id".equals(key) || "tenant_ref_no".equals(key)) {
                } else {
                    // 必填项为空时返回错误
                    if (headElements.get(key).isEmpty()) {
                        return setRtnMsg(449,key + " isEmpty");
                    }
                }
            }
            headElements.remove("partner_sign");
            // 检查Head内容完整性
            String mymd5 = MD5Util.encodeCollection(headElements.values());
            String comingMd5 = root.element("Head").element("partner_sign").getText();
    //      System.out.println("mymd5>>" + mymd5 + "<<" + root.element("Head").element("partner_sign").getText());
            if (mymd5.equalsIgnoreCase(comingMd5)) {
                ComingMessage msg = new ComingMessage(doc.asXML(),new Date().getTime());
                try {
                    comingMessageMapper.insert(msg);
    //              messageRepository.save(msg);
                } catch (Exception e) {
                    return setRtnMsg(500,"save fail");
                }
            } else {
                return setRtnMsg(449,"ContentMD5 Unmatch.get:" + mymd5 + ":::partner_sign:" + comingMd5 + ":::" + doc.asXML());
            }
    
            return setRtnMsg(202,"Success");
    

    json

    BufferedReader br = new BufferedReader(new InputStreamReader(
                        (ServletInputStream) request.getInputStream()));
                String line = null;
                StringBuffer sb = new StringBuffer();
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                String appMsg=sb.toString();
    

    获取到了json字符串,然后就可以转bean了

    相关文章

      网友评论

          本文标题:request读流,判断不同的请求

          本文链接:https://www.haomeiwen.com/subject/fbsxxxtx.html