系统与系统之间WebService的应用还是挺多的。尤其是webService其跨平台的特性,更是让人迷恋,既方便又简洁。下面发一下在实际开发过程中所用到的一个webservice接口的实例。
这是一个价格管理系统调用另一个系统的webservice接口。
前台我们采用输入报文然后点击调用接口的方式,我们先来看一下前端页面
在文本框中输入一下报文,点击按钮就会调用webservice接口,与另一系统连接。
js代码段
function ToBfoInteface(){ showMask(); var testXmlStr = $("#TestBFOInterface").val(); if(testXmlStr==null||testXmlStr==""){ alert("请填写接口报文!"); } $.ajax({ url: appUrl + '/test/toBFOInterface', contentType:'application/xml;charset="UTF-8"', type:"post", dataType:"text", processData: false, data : testXmlStr, async:false, success : function(responseText) { hideMask(); var text = ""; try{ text = responseText; }catch(e){ text = "error" } showMsg(text,"调取bfo接口结果:"); initMenuDataNum(); } }); }后台代码段
这是HttpKit类的工具方法 public static Map<String, String> postSoap3(String url, String xml) { Map<String, String> resultMap = new HashMap<String, String>(); // 返回字符串 String rs = null; // 返回状态 String resultStatusCode = ""; // 创建httppost HttpPost httpPost = null; // 创建参数队列 CloseableHttpClient httpClient = null; try { String msg = ""; if(Log.msetlog.isDebugEnabled()){ if(xml.indexOf("<TaskId>") > 0){//审批时调用 msg = "; 审批时相关TaskId :" + xml.substring(xml.indexOf("<TaskId>")+8, xml.indexOf("</TaskId>")); }else if(xml.indexOf("<def:EspaId>") > 0){ msg = "; SEQID:" + xml.substring(xml.indexOf("<def:EspaId>")+12, xml.indexOf("</def:EspaId>")); }else if(xml.indexOf("<espaId>") > 0){ msg = "; SEQID:" + xml.substring(xml.indexOf("<espaId>")+8, xml.indexOf("</espaId>")); }else if(xml.indexOf("<wsse:Username>") > 0){ msg = "; UserName:" + xml.substring(xml.indexOf("<wsse:Username>")+15, xml.indexOf("</wsse:Username>")); } } if ("https".equals(url.substring(0, 5))) { // 使用连接池 // httpClient=createSSLInsecureClient(); // 不使用连接池 Log.msetlog.debug("创建https对应httpClient开始时间:"+DateKit.getNowTime()+msg); httpClient = createSSLClientDefault(); Log.msetlog.debug("创建https对应httpClient结束时间:"+DateKit.getNowTime()+msg); } else { // httpClient=HttpClients.custom().setConnectionManager(cm).build(); Log.msetlog.debug("创建httpClient开始时间:"+DateKit.getNowTime()+msg); httpClient = HttpClients.createDefault(); Log.msetlog.debug("创建httpClient结束时间:"+DateKit.getNowTime()+msg); } Log.msetlog.debug("创建httpPost开始时间:"+DateKit.getNowTime()+msg); httpPost = new HttpPost(url); Log.msetlog.debug("创建httpPost结束时间:"+DateKit.getNowTime()+msg); Log.msetlog.debug("设置httpPost属性开始时间:"+DateKit.getNowTime()+msg); HttpEntity re = new StringEntity(xml, "UTF-8"); httpPost.setHeader("Content-Type", "application/soap+xml; charset=utf-8"); httpPost.setEntity(re); Log.msetlog.debug("设置httpPost属性结束时间:"+DateKit.getNowTime()+msg); Log.msetlog.debug("最终执行调用接口开始时间:"+DateKit.getNowTime()+msg); CloseableHttpResponse response = httpClient.execute(httpPost); Log.msetlog.debug("最终执行调用接口结束时间:"+DateKit.getNowTime()+msg); if (response != null) { resultStatusCode = String.valueOf(response.getStatusLine() .getStatusCode()); } try { HttpEntity entity = response.getEntity(); if (entity != null) { rs = EntityUtils.toString(entity, "UTF-8"); } } finally { response.close(); httpPost.abort(); } } catch (Exception e) { Log.soaplog.debug("error", e); e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpClient.close(); } catch (Exception e) { Log.soaplog.debug("error", e); e.printStackTrace(); } } resultMap.put("ResultStatus", resultStatusCode); resultMap.put("ResultXML", rs); return resultMap; }下面这段代码用来获取xml报文的某个节点的值
public static String getNodeByXml(String xml, String node) { Document doc = convertDocFromStr(xml); if (doc == null) { return null; } else { Node typeNode = doc.selectSingleNode(".//*[local-name()='" + node + "']"); if (typeNode == null) { return null; } else { return typeNode.getStringValue(); } } }返回带参的xml数据
public static String textBfo(String xml){ Map<String, String> resultMap = HttpKit.postSoap3(bfo_webservice_url,xml); String msg = resultMap.get("ResultXML");//返回XML String message = HttpKit.getNodeByXml(msg,"message"); return message; }controller层
@RequestMapping("/toBFOInterface") @ResponseBody public Object toBFOInterface(HttpServletRequest request,HttpServletResponse response,@RequestBody String requestBody) { String xml = null; try { xml = URLDecoder.decode(requestBody,"UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String msg = BFOHttpKit.textBfo(xml);//返回XML return msg; }至此,webservice接口已开发完毕。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |