在調用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);
}
}