在调用web service接口时,根据网上查找的demo,都有问题,经过1天的研究,终于明白了调用web service的原理,下面例子便是测试的demo,比较容易理解。
1.首先配置pom文件:
<!--spring web Service的包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<!--spring web service wsdl包-->
<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>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
<!-- axis 1.4 jar end -->
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Vector;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.handlers.soap.SOAPService;
public class Test3 {
public static void main(String[] args) throws RemoteException, ServiceException, MalformedURLException {
//定义wsdl路径
String endpoint = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
//wsdl路径
call.setTargetEndpointAddress(new java.net.URL(endpoint));
//方法名称
call.setOperationName(new QName("http://WebXml.com.cn/", "getSupportCityString"));
//参数
call.addParameter(new QName("http://WebXml.com.cn/","theRegionCode"),org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
//启用soap
call.setUseSOAPAction(true);
//返回参数的类型(不能用Array,否则报错)
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_VECTOR);
//接口中调用方法的soapAction
call.setSOAPActionURI("http://WebXml.com.cn/getSupportCityString");
SOAPService soap = new SOAPService();
//接口中定义的webservice名称
soap.setName("WebServices");
call.setSOAPService(soap);
Vector ret = (Vector) call.invoke(new Object[]{"北京"});
System.out.println("--------"+ret);
}
}
来源:CSDN
作者:smilejuan
链接:https://blog.csdn.net/u010722889/article/details/82769782