java调用webService接口,入参和出差均为xml报文,出差转为json输出前台

1、拼接入参报文,调用方法,处理出参报文,格式化为list,通过@ResponseBody格式化为json输出前台。出参报文不同,处理方法不同,本文以我自己的出参报文为例进行处理

@RequestMapping(value="/toData.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getData(String serviceName,String startTime,String endTime,String latn) throws DocumentException {
		//入参报文
		String inBody = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:inv=\"http://invoke.ws.io.starit.com\">\n" +
				"   <soapenv:Header/>\n" +
				"   <soapenv:Body>\n" +
				"      <inv:startInvoke soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
				"         <xml xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
				"                 <![CDATA[\n" +
				"\t\t\t\t\t<msg>\n" +
				"\t\t\t\t\t<router>\n" +
				"\t\t\t\t\t<from>SAS</from>\n" +
				"\t\t\t\t\t<to>oss_wms</to>\n" +
				"\t\t\t\t\t<time>2018-01-14 18:07:19</time>\n" +
				"\t\t\t\t\t<serviceName>BTFB_Quality_Count</serviceName>\n" +
				"\t\t\t\t\t<msgId>1515924439799</msgId>\n" +
				"\t\t\t\t\t</router>\n" +
				"\t\t\t\t\t<data>\n" +
				"\t\t\t\t\t<TIME_START>"+startTime+"</TIME_START>\n" +
				"\t\t\t\t\t<TIME_END>"+endTime+"</TIME_END>\n" +
				"\t\t\t\t\t<LATN>"+latn+"</LATN>\n" +
				"\t\t\t\t\t</data>\n" +
				"\t\t\t\t\t</msg>\n" +
				"\t\t\t\t ]]>\n" +
				"         </xml>\n" +
				"      </inv:startInvoke>\n" +
				"   </soapenv:Body>\n" +
				"</soapenv:Envelope>\n";
		String urlStr = "http://135.146.16.38:10010/axis/services/ServicesRouter?wsdl";
		String xmlStr = testPost(urlStr, inBody);
		System.out.println("==================="+inBody+"=====================");
		System.out.println("原始返回报文字符串:" +xmlStr); // 拿到根节点的名称
		System.out.println("========================================"); 
		String new_xmlStr=xmlStr.substring(xmlStr.indexOf("<data>")+6,xmlStr.indexOf("</data>"));
		String new_xmlStr2="<data>"+new_xmlStr+"</data>";
		System.out.println("截取后的:"+new_xmlStr2);
		SAXReader reader = new SAXReader();
		Document document = DocumentHelper.parseText(new_xmlStr2);
		Element root = document.getRootElement();
		List<Element> list = root.selectNodes("//entity");
		List<Diagnosis> list_order=new ArrayList<Diagnosis>();
		for(Element ele:list){
			Diagnosis diagnosis=new Diagnosis();
			diagnosis.setCREATE_DT(ele.selectSingleNode("CREATE_DT").getText());
			diagnosis.setCREATE_TIME(ele.selectSingleNode("CREATE_TIME").getText());
			diagnosis.setFAULT_PHE(ele.selectSingleNode("FAULT_PHE").getText());
			diagnosis.setLATN_NAME(ele.selectSingleNode("LATN_NAME").getText());
			diagnosis.setOBLIGATE_D(ele.selectSingleNode("OBLIGATE_D").getText());
			diagnosis.setOBLIGATE_E(ele.selectSingleNode("OBLIGATE_E").getText());
			diagnosis.setTIME1(ele.selectSingleNode("TIME1").getText());
			diagnosis.setVILLAGENAME(ele.selectSingleNode("VILLAGENAME").getText());
			list_order.add(diagnosis);
		}
        Map<String, Object> data = new HashMap<String, Object>();
		data.put("total", list_order.size());
		data.put("rows", list_order);
		System.out.println(data.toString());
		return data;
	}
   /**
     *调用接口方法
     * @param urlStr 地址
     * @param inBody 报文
     * @return
     */
    public  String testPost(String urlStr,String inBody) {
        String result = "";
        try {
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            //con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            con.setRequestProperty("SOAPAction","");

            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
            out.write(new String(inBody.getBytes("UTF-8")));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String line = "";
            for (line = br.readLine(); line != null; line = br.readLine()) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.replace("&lt;","<")
                .replace("&gt;",">")
                .replace("&quot;","\"");
    }

猜你喜欢

转载自blog.csdn.net/qq_28635317/article/details/105844574