一、用到的依赖包(可以在mevan中央仓库自行下载 https://mvnrepository.com/)
wsdl4j wsdl4j1.6.3 org.apache.axis axis1.4 org.dom4j dom4j2.1.1
二、需求描述*
通过java程序远程访问地方服务提供的webservice服务,调用是参数四个入参
由于xml消息没有格式化,为了调用及后续日志清晰,所以在调用前,首先判断了该字符串是不是xml,如是则格式化成标准xml消息体,主要为了提高日志消息的可阅读性
三、代码样例:
package cn.tx.webservice; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.handlers.soap.SOAPService; import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import org.xml.sax.InputSource; import javax.xml.parsers.documentBuilder; import javax.xml.parsers.documentBuilderFactory; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import java.io.StringReader; import java.io.StringWriter; import java.rmi.RemoteException; public class SendSynyiPatientCardAdd { //设置访问wsdl服务地址 private final String url="http://100.89.xx.xx:22002/services/HIS?wsdl"; //命名空间 private final String namespace="www.xxx.com"; //具体方法 private final String method="invoke"; //设置接口名称 private final String soapAction="H"; public String sendPatientAdd(String services,String urid,String pwd,String parameter) throws ServiceException, ServiceException, RemoteException { Service service=new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url);//设置请求wsdl call.setOperationName(new QName(namespace,method)); //设置请求命名空s间及具体方法 //设置入参 call.addParameter("services", XMLType.XSD_STRING, ParameterMode.IN);//设置请求参数1 call.addParameter("urid", XMLType.XSD_STRING, ParameterMode.IN);//设置请求参数2 call.addParameter("pwd", XMLType.XSD_STRING, ParameterMode.IN);//设置请求参数3 call.addParameter("parameter", XMLType.XSD_STRING, ParameterMode.IN);//设置请求参数4 call.setUseSOAPAction(true); //设置使用soap *** 作 call.setTimeout(6000); //设置超时时间 call.setReturnType(XMLType.XSD_STRING);//设置返回类型 SOAPService soapService=new SOAPService(); soapService.setName(soapAction); call.setSOAPService(soapService); Object[] obj = { services, urid, pwd,parameter}; //将所需要传过去的四个参数放在数据组内 String invoke = (String)call.invoke(obj);//调用并将返回强转字符串 return invoke; } public static void main(String[] args) throws Exception { SendSynyiPatientCardAdd send=new SendSynyiPatientCardAdd(); String xml=""; //需自定义参数 String isxml = formatXml(xml);//将消息格式化xml String st = send.sendPatientAdd("PatientCardUpdate", "H", "h", isxml); String responseXml = formatXml(st);//将返回消息格式化xml System.out.println(responseXml); } //判断字符串是否符合xml格式要求 public static boolean isXmldocument(String xml){ boolean isxml=true; documentBuilderFactory documentBuilderFactory = documentBuilderFactory.newInstance(); documentBuilder documentBuilder=null; try { documentBuilder= documentBuilderFactory.newdocumentBuilder(); documentBuilder.parse(new InputSource(new StringReader(xml))); } catch (Exception e) { isxml=false; } return isxml; } //将字符串字符串的xml 进行格式化 public static String formatXml(String xml) throws Exception { boolean isxml = isXmldocument(xml); if(isxml==false){ return "is not xml"; } org.dom4j.document document= documentHelper.parseText(xml); OutputFormat outputFormat=OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); StringWriter stringWriter=new StringWriter(); //格式化输出 XMLWriter xmlWriter=new XMLWriter(stringWriter,outputFormat); xmlWriter.write(document); return stringWriter.toString(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)