通过java 调⽤webservice 服务详细案例⼀
前⾔
1)通过⼀个简单的java案例如何调⽤第三⽅提供的web服务,想想⾃⼰测试接⼝的时候⽤过,可能对有些新伙伴有些帮助。所以还是记录⼀下吧。如有不⾜,请指教。。。。。
⼀、⽤到的依赖包
⼆、说明
三、代码样例 <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency> <!-- axis 1.4 jar start --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <!-- 处理xml--> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.1</version> </dependency>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1、通过java 程序远程访问地⽅服务提供的webservice 服务,需要传⼊四个参数 2、通过读取本地⽂件,并判断是否符合xml 格式 3、如符合,则格式化xml ,提⾼阅读性
1
2
3package 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 .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 .BufferedReader ;import java .io .FileReader ;import java .io .StringReader ;import java .io .StringWriter ;import java .rmi .RemoteException ;/**
* @ClassName SendSynyiPatientCardAdd
1
2
3
4
5
6
7
8
9
10
11
12
dom4j读取xml13
14
15
16
17
18
19
20
21
22
23
24
* @ClassName SendSynyiPatientCardAdd * @Description TODO * @Author jiac * @Date 2021/11/5 14:08 * @Version 1.0 **/public class SendSynyiPatientCardAdd { //设置访问wsdl 服务地址 private final String url ="localhost:22002/services/HIS?wsdl"; //命名空间 private final String namespace ="www.synyi"; //具体⽅法 private final String method ="invoke"; //设置接⼝名称 // private final String soapAction=""; 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); //设置超时时间 cal
l .setReturnType (XMLType .XSD_STRING );//设置返回类型 Object [] obj = { services , urid , pwd ,parameter }; String invoke = (String )call .invoke (obj ); return invoke ; } //判断是否符合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 ; } //将字符串string 的xml 进⾏格式化 public static String formatXml (String xml ) throws Exception { boolean isxml = isXmlDocument (xml ); if (isxml ==false ){ return "false"; } 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 (); } public static void main (String [] args ) throws Exception { SendSynyiPatientCardAdd send =new SendSynyiPatientCardAdd ();
252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
1.结尾 SendSynyiPatientCardAdd send =new SendSynyiPatientCardAdd (); StringBuffer sbf = new StringBuffer (); BufferedReader bufferedReader =new BufferedReader (new FileReader ("D:\\MAVEN\\tx_boot\\springboot_first\\src\\l" String xml =""; while ( (xml =(bufferedReader .readLine ()))!=null ){ sbf .append (xml ); } String isxml = formatXml (sbf .toString ()); if (isxml !="false"){ String st = send .sendPatientAdd ("InhospitalRegistrationUpdate", "HIS", "HIS", isxml ); String responseXml = formatXml (st ); System .out .println ("Response:"+"\r\n"+responseXml ); } return ; } }
90919293949596979899100101102103104105106107108 总之,通过这个简单的案例展⽰,主要是为了记录下,调⽤第三⽅的提供的web 服务,当第三⽅提供web 服务后,我们可以将这个服地址(localhost:220 放在浏览器打开,需要关注 名称空间名称 、⽅法名称 、以及需要传⼊的参数。已经参数类型,通常都是string.12
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论