JAVA调用Web Service接口的五种方式 您所在的位置:网站首页 soap调用webservice JAVA调用Web Service接口的五种方式

JAVA调用Web Service接口的五种方式

2023-03-17 18:41| 来源: 网络整理| 查看: 265

1. 通过eclipse等直接生成Client

电话归属地接口:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

创建JAVA项目→在该项目中创建Web Service Client→填入wsdl→无报错生成成功 - 不用管生成的Client代码,直接阅读wsdl文档 - wsdl由下到上阅读 - 强调不用管生成的代码,直接新建一个类开始写测试代码

归属地接口调用代码如下:

//:Call.java import cn.com.webxml.MobileCodeWS; import cn.com.webxml.MobileCodeWSSoap; public class Call { public static void main(String[] args) { MobileCodeWS mobileCode=new MobileCodeWS(); MobileCodeWSSoap mw= mobileCode.getMobileCodeWSSoap(); String returninfo = mw.getMobileCodeInfo("18509391234", null); System.out.println(returninfo); } }2. 通过JAVA命令wsimprot命令生成Clientwsimport -d D:\tianqi -s D:\tianqi\s -p com.tianqi -verbose http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 参数说明 -keep:是否生成java源文件 -d:指定.class文件的输出目录 -s:指定.java文件的输出目录 -p:定义生成类的包名,不定义的话有默认包名 -verbose:在控制台显示输出信息 -b:指定jaxws/jaxb绑定文件或额外的schemas将生成的代码copy到新建的java项目中阅读wsdl,写测试代码测试代码如:方式1 Call.java3.通过axis直接调用(无需生成Client)

这种方法需要非常了解wsdl,通过阅读wsdl就能知道其中的方法,参数等等,然后就可以直接撸代码啦

//:CallAxia.java import javax.xml.rpc.ParameterMode; import javax.xml.rpc.encoding.XMLType; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class CallAxis { public static void main(String[] args) { try { //直接引用远程的wsdl文件删除了后面的?wsdl String endpoint = "http://127.0.0.1:8088//services/Test"; //以下都是套路 Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName("TestMethod");//WSDL里面描述的方法名 TestMethod call.addParameter("in0",XMLType.SOAP_STRING,ParameterMode.IN);//接口的参数 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 String [] temp = {"111"}; Object[] o = {temp}; String result = (String) call.invoke(o);//给方法传递参数,并且调用方法 System.out.println(result); //打印接口的返回值 } catch (Exception e) { System.err.println(e.toString()); } } }

需要依赖:axis-ant.jar,axis.jar ,commons-discovery.jar ,commons-logging.jar ,jaxrpc.jar ,log4j-1.2.8.jar ,saaj.jar ,wsdl4j.jar

4.通过axis中的wsdl2java工具生成client

天气预报接口:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl - 在axis官网下载axis2-1.4.1-bin.zip并解压至D盘 - axis百度云:链接:https://pan.baidu.com/s/1LiOp18mLVouUe3fXqTR6Ng 提取码:fa75 - 设置环境变量: 新建环境变量:axishome 变量值:D:\axis2-1.4.1 - 在path中新增变量值: %axishome%\bin - 在命令行中输入如下命令:

wsdl2java.bat -uri http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl -o src -p tianqi 参数说明(常用): -o : 指定生成代码的输出路径 -p : 指定代码的package名称 -l : 使用的语言(Java/C) 默认是java -a : 生成异步模式的代码 -s : 生成同步模式的代码 -t : 为代码生成测试用例 -g : 生成服务端和客户端的代码 -ss :生成服务端代码 默认不生成把生成的包导入项目生成文件中有个xxxxstub.java 创建一个它的对象这是就能看到它里面的方法了,开始撸代码如下:这种方式需要的依赖较多一定不要漏了(我把lib的全部导入了具体不知道需要哪些)//:TianQi.java import java.rmi.RemoteException; import org.apache.axis2.AxisFault; import tianqi.WeatherWebServiceStub; import tianqi.WeatherWebServiceStub.ArrayOfString; import tianqi.WeatherWebServiceStub.GetWeatherbyCityName; import tianqi.WeatherWebServiceStub.GetWeatherbyCityNameResponse; public class TianQi { public static void main(String[] args) { try { WeatherWebServiceStub wws = new WeatherWebServiceStub(); GetWeatherbyCityName getweather = new GetWeatherbyCityName(); getweather.setTheCityName("安康"); try { GetWeatherbyCityNameResponse returninfo1 = wws .getWeatherbyCityName(getweather); ArrayOfString a = returninfo1.getGetWeatherbyCityNameResult(); String[] dd = a.getString(); // 将ArrayOfString 转为 String数组 for (int i = 0; i < dd.length; i++) { // 遍历数组 System.out.println(dd[i]); //打印接口的返回值 } } catch (RemoteException e) { // TODO Auto-generated catch block System.out.println("接口调用失败"); e.printStackTrace(); } } catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); } } }5.直接SOAP调用远程的webservice

这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来

import org.apache.soap.util.xml.*; import org.apache.soap.*; import org.apache.soap.rpc.*; import java.io.*; import java.net.*; import java.util.Vector; public class caService { public static String getService(String user) { URL url = null; try { url = new URL( "http://192.168.0.100:8080/ca3/services/caSynrochnized"); } catch (MalformedURLException mue) { return mue.getMessage(); } // This is the main SOAP object Call soapCall = new Call(); // Use SOAP encoding soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); // This is the remote object we're asking for the price soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized"); // This is the name of the method on the above object soapCall.setMethodName("getUser"); // We need to send the ISBN number as an input parameter to the method Vector soapParams = new Vector(); // name, type, value, encoding style Parameter isbnParam = new Parameter("userName", String.class, user, null); soapParams.addElement(isbnParam); soapCall.setParams(soapParams); try { // Invoke the remote method on the object Response soapResponse = soapCall.invoke(url, ""); // Check to see if there is an error, return "N/A" if (soapResponse.generatedFault()) { Fault fault = soapResponse.getFault(); String f = fault.getFaultString(); return f; } else { // read result Parameter soapResult = soapResponse.getReturnValue(); // get a string from the result return soapResult.getValue().toString(); } } catch (SOAPException se) { return se.getMessage(); } } }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有