一个简单的AXIS远程调用Web Service示例
我们通常都将编写好的Web Service发布在Tomcat或者其他应用服务器上,然后通过浏览器调用该Web Service,返回规范的XML文件。但是如果我们不通过浏览器调用,而是通过客户端程序调用,该如何实现?
接下来,我们利用Eclipse作为开发工具,演示一个Axis调用WebService的简单示例。步骤如下:
第一步:新建Web Project (一定要是web project,不能是java project)。
第二步:导入AXIS类库。(官方下载:http://apache.etoak.com//axis/axis2/java/core/1.5.4/axis2-1.5.4-bin.zip)(即把下载包里../lib/目录下的jar文件拷贝到工程的classpath下。 ) ,使用maven的话,此步忽略。
第三步:新建一个简单的连接字符串的类HelloWorld.java:
- package com.mzh.webservice;
- public class HelloWorld {
- public String connectStr(String str1,String str2,int flag){
- String resultStr="no str";
- if(flag==1){
- resultStr=str1+"---"+str2;
- }else if(flag==2){
- resultStr=str2+"---"+str1;
- }
- System.out.println(resultStr);
- return resultStr;
- }
- }
第四步:右击HelloWorld.java---Web Services---Create Web service.依次点击下一步……finish。此时发现WebContent目录下生成一个wsdl目录,目录下有一个HelloWorld.wsdl
第五步:测试web service :右击HelloWorld.wsdl---Web Services---Test with Web Services Explorer---点击方法名connectStr----输入参数mzh,zyd,1---点go.此时会发现控制台输出mzh---zyd,说明测试成功。WebService没有问题。
第六步:编写客户端调用类,利用AXIS远程调用HelloWorld.(为了体现远程调用,把此类放到另外一个工程里去)代码如下:
- import java.rmi.RemoteException;
- import javax.xml.rpc.ServiceException;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- public class HelloWorldTest {
- public String invokeRemoteFuc(){
- String endpoint=
- "http://192.168.1.236:8080/Axis2/services/HelloWorld";
- String result ="no result!";
- Service service = new Service();
- Call call;
- try {
- call=(Call)service.createCall();
- call.setTargetEndpointAddress(endpoint);//远程调用路径
- call.setOperationName("connectStr");//调用的方法名
- //设置参数名:
- call.addParameter("str1", //参数名
- org.apache.axis.encoding.XMLType.XSD_STRING,//参数类型:String
- javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'
- call.addParameter("str2", //参数名
- org.apache.axis.encoding.XMLType.XSD_STRING,//参数类型:String
- javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'
- call.addParameter("flag", //参数名
- org.apache.axis.encoding.XMLType.XSD_INT,//参数类型:INT
- javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'
- //设置返回值类型:
- call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//返回值类型:String
- String str1="mzh";
- String str2="zyd";
- int flag=1;
- result = (String)call.invoke(new Object[]{str1,str2,flag});//远程调用
- } catch (ServiceException e) {
- e.printStackTrace();
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- return result;
- }
- //测试:
- public static void main(String[] args){
- HelloWorldTest test = new HelloWorldTest();
- String result = test.invokeRemoteFuc();
- System.out.println(result);
- }
- }
(注意:1、设置方法名和参数名的时候,必须与被调用的WebService保持一致;2、传递的参数需封装到了一个Object数组里)
第七步:测试:HelloWorldTest ---Run as---Java application,如果发现控制台正确输出 mzh---zyd.说明测试成功。
第八步:至此,大功告成!
以下为pom.xml文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>WS_AXIS_TEST</groupId> <artifactId>WS_AXIS_TEST</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.axis/axis --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.axis/axis-jaxrpc --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-jaxrpc</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery --> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> </dependency> <!-- https://mvnrepository.com/artifact/wsdl4j.wso2/wsdl4j --> <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j --> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.activation/activation --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.4</version> </dependency> </dependencies> </project>