zoukankan      html  css  js  c++  java
  • 一个简单的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: 

    Java代码  收藏代码
    1. package com.mzh.webservice;  
    2. public class HelloWorld {  
    3.       
    4.     public String connectStr(String str1,String str2,int flag){  
    5.         String resultStr="no str";  
    6.         if(flag==1){  
    7.             resultStr=str1+"---"+str2;  
    8.         }else if(flag==2){  
    9.             resultStr=str2+"---"+str1;  
    10.         }  
    11.         System.out.println(resultStr);  
    12.         return resultStr;  
    13.     }  
    14. }  


         第四步:右击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.(为了体现远程调用,把此类放到另外一个工程里去)代码如下: 

    Java代码  收藏代码
    1. import java.rmi.RemoteException;  
    2. import javax.xml.rpc.ServiceException;  
    3. import org.apache.axis.client.Call;  
    4. import org.apache.axis.client.Service;  
    5.   
    6. public class HelloWorldTest {  
    7.    public String invokeRemoteFuc(){  
    8.       String endpoint=  
    9.             "http://192.168.1.236:8080/Axis2/services/HelloWorld";  
    10.       String result ="no result!";  
    11.       Service service = new Service();  
    12.       Call call;  
    13.       try {  
    14.           call=(Call)service.createCall();  
    15.           call.setTargetEndpointAddress(endpoint);//远程调用路径  
    16.      call.setOperationName("connectStr");//调用的方法名  
    17.       //设置参数名:  
    18.        
    19.    call.addParameter("str1", //参数名  
    20.     org.apache.axis.encoding.XMLType.XSD_STRING,//参数类型:String  
    21.     javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'  
    22.       
    23.    call.addParameter("str2", //参数名  
    24.     org.apache.axis.encoding.XMLType.XSD_STRING,//参数类型:String  
    25.     javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'  
    26.       
    27.    call.addParameter("flag", //参数名  
    28.     org.apache.axis.encoding.XMLType.XSD_INT,//参数类型:INT  
    29.     javax.xml.rpc.ParameterMode.IN);//参数模式:'IN' or 'OUT'  
    30.       //设置返回值类型:  
    31.     call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//返回值类型:String  
    32.     String str1="mzh";  
    33.     String str2="zyd";  
    34.     int flag=1;  
    35.     result = (String)call.invoke(new Object[]{str1,str2,flag});//远程调用  
    36.      } catch (ServiceException e) {  
    37.     e.printStackTrace();  
    38.      } catch (RemoteException e) {  
    39.     e.printStackTrace();  
    40.      }  
    41.     return result;  
    42.    }  
    43. //测试:  
    44.  public static void main(String[] args){  
    45.       HelloWorldTest test = new HelloWorldTest();  
    46.       String result = test.invokeRemoteFuc();  
    47.       System.out.println(result);  
    48.   }  
    49.   
    50. }  


       (注意: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>
  • 相关阅读:
    Poj 2017 Speed Limit(水题)
    Poj 1316 Self Numbers(水题)
    Poj 1017 Packets(贪心策略)
    Poj 1017 Packets(贪心策略)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2000 Gold Coins(水题)
    poj 2000 Gold Coins(水题)
  • 原文地址:https://www.cnblogs.com/toSeeMyDream/p/6650044.html
Copyright © 2011-2022 走看看