1.调用案例,仅供参考 axis api 调用 使用 axis api 调用接口,调用 WeatherWebService (公共的 webservice 服务),网址参看WeatherWebService axis 依赖 + junit jar 包 gradle 配置 dependencies { testCompile group:
1.调用案例,仅供参考
axis api 调用
使用 axis api 调用接口,调用 WeatherWebService (公共的 webservice 服务),网址参看 WeatherWebService
axis 依赖 + junit jar 包 gradle 配置
dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' compile group: 'org.apache.axis', name: 'axis', version: '1.4' compile group: 'javax.xml', name: 'jaxrpc-api', version: '1.1' compile group: 'commons-logging', name: 'commons-logging', version: '1.0.3' compile group: 'commons-discovery', name: 'commons-discovery', version: '0.2' compile group: 'wsdl4j', name: 'wsdl4j', version: '1.6.2' compile group: 'javax.mail', name: 'mail', version: '1.4' compile group: 'javax.activation', name: 'activation', version: '1.1.1' }
直接上 test 测试代码,亲测可用:
package com.ycit.webservice; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.junit.Test; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import java.util.Vector; /** * Created by xlch on 2016/11/24. */ public class CallInterface { @Test public void getWeather() { String endPoint = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL";//提供接口的地址,也可去掉后面的 "?WSDL" String targetNamespace = "http://WebXml.com.cn/"; String methodName = "getWeatherbyCityName"; String soapAction = targetNamespace + methodName; //详情打开 endPoint链接 搜索 soapaction 即可查看 soapaction String city = "苏州"; Service service = new Service(); try { Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endPoint); call.setOperationName(new QName(targetNamespace, methodName)); //设置要调用哪个方法 call.addParameter(new QName(targetNamespace, "theCityName"), XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(new QName(targetNamespace, methodName), Vector.class); //要返回的数据类型(自定义类型) // call.setReturnType(XMLType.XSD_STRING);//(标准的类型) call.setUseSOAPAction(true); call.setSOAPActionURI(soapAction); Vector v = (Vector) call.invoke(new Object[]{city});//调用方法并传递参数 for (int i = 0; i < v.size(); i++) { System.out.println(v.get(i)); } } catch (Exception ex) { ex.printStackTrace(); } } }
输出结果:
江苏 苏州 58357 58357.jpg 2016-11-24 15:12:14 2℃/9℃ 11月24日 多云转阴 北风3-4级转微风 1.gif 2.gif 今日天气实况:气温:7℃;风向/风力:东北风 2级;湿度:55%;紫外线强度:最弱。空气质量:良。 紫外线指数:最弱,辐射弱,涂擦SPF8-12防晒护肤品。 感冒指数:较易发,天较凉,增加衣服,注意防护。 穿衣指数:较冷,建议着厚外套加毛衣等服装。 洗车指数:较适宜,无雨且风力较小,易保持清洁度。 运动指数:较不宜,天气寒冷,推荐您进行室内运动。 空气污染指数:良,气象条件有利于空气污染物扩散。 7℃/11℃ 11月25日 阴转阵雨 东北风转东风微风 2.gif 3.gif 4℃/9℃ 11月26日 阵雨转多云 西北风微风 3.gif 1.gif 苏州位于长江下游,太湖之滨,鱼米之乡,面积8488平方公里。下辖张家港,常熟,太仓,昆山,吴县,吴江六县市,人口575万。其为亚热带季风气候,四季分明,全年平均气温15-17度,1月最低气温-9.4度,7、8月份最高气温达38.6度;全年平均降水量偏高,近1076.2毫米,无霜期在230天左右。苏州全市地势低平,平原占总面积的54%,平均海拔4米左右,东南部地势低洼,西南部多小山丘。吴县穹窿山主峰高351.7米,为全市最高点。境内河流纵横,湖泊众多,京杭运河贯通南北,望虞河、娄江、太浦河等连接东西,阳澄湖、昆承湖、淀山湖等散布其间,太湖水面90%左右在苏州市境内,全市水面占总面积的42.5%,是著名的江南水乡。苏州古城内现有河道35公里、桥梁168座,被誉为"东方威尼斯"。苏州园林甲天下,已被列入世界文化遗产名录,在中国四大名园中,苏州就占有两席(拙政园、留园)。“吴中第一名胜”虎丘,2500多年的历史形成的深厚的文化积淀,成为游客来苏州旅游的必游之地。而唐朝诗人张继的一首《枫桥夜泊》,使古今游客都要来枫桥,闻寒山寺的钟声。景观:苏州,拙政园,网师园,狮子林,留园,沧浪亭,怡园枫桥,寒山寺 Disconnected from the target VM, address: '127.0.0.1:59418', transport: 'socket' Process finished with exit code 0
使用生成的 java 代码访问
使用 axis 生成 java 代码,然后使用代码访问
生成 java 代码 方法(基于 intellij idea):
Ⅰ 创建项目
Ⅱ 右击项目,选择最底部的 Webservices 按钮 → Generate Java Code From Wsdl 即可打开生成代码的 设置框。
Ⅲ 输入 wsdl url ,选择输出路径,设置包名前缀,选择 Web Service 的平台(支持多种,前提是提前配好插件,这里选择 Apache Axis),其他默认,点 OK 即可。
这里仍然以 WeatherWebService 为例:
wsdl url :
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?WSDL
生成后的代码如下:
junit 测试调用 java 代码 完成请求
package com.ycit.webservice; import com.ycit.WeatherWebService; import com.ycit.WeatherWebServiceLocator; import com.ycit.WeatherWebServiceSoap_PortType; import org.junit.Test; /** * Created by xlch on 2016/11/24. */ public class CallJavaCode { @Test public void getSupportProvince() throws Exception { WeatherWebService service = new WeatherWebServiceLocator(); WeatherWebServiceSoap_PortType soap = service.getWeatherWebServiceSoap(); String[] results = soap.getSupportProvince(); for (String result : results) { System.out.println(result); } } @Test public void getWeather() throws Exception { WeatherWebService service = new WeatherWebServiceLocator(); WeatherWebServiceSoap_PortType soap = service.getWeatherWebServiceSoap(); String[] results = soap.getWeatherbyCityName("苏州"); for (String result : results) { System.out.println(result); } } }