Web Service AXIS2入门示例

环境: jdk1.5、tomcat5.5;

首先到官方网站下载AXIS2相关jar包;

执行类 SimpleService:

public class SimpleService
{
    public String helloWorld(String name)
    {
        return "你好 " + name;
    }   
       
    public String getInfoTest(){
     return "出来的感觉真的可以这样》》》";
    }
}

WEB-INF/services/axis/META-INF/services.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<service name="Hello" scope="application">
<description>axis2入门</description>
<messageReceivers>
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
   class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
   class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>

<!--  配置业务实现类   -->
<parameter name="ServiceClass">com.yss.ws.SimpleService</parameter>
</service>



web.xml 文件将Axis2 WEB-INFO 下的copy过来就OK。

测试类:



import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;



public class RPCClientTest
{
    public static void main(String[] args) throws Exception 
    {
        //  使用RPC方式调用WebService       
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        //  指定调用WebService的URL
        EndpointReference targetEPR = new EndpointReference( "http://192.168.72.41:8080/axis_yss/services/Hello?wsdl");
        //设置wsdl访问路劲

       options.setTo(targetEPR);
        //  指定getGreeting方法的参数值
        Object[] opAddEntryArgs = new Object[] {"hello world"};
        //  指定getGreeting方法返回值的数据类型的Class对象
        Class[] classes = new Class[] {String.class};
        //  指定要调用的getGreeting方法及WSDL文件的命名空间
        QName opAddEntry = new QName("http://ws.yss.com", "getInfoTest ");
        //  调用getGreeting方法并输出该方法的返回值
        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
         //设置传入参数类型以及数量

        classes = new Class[] {String.class};

        //设置wsdl中的targetNamespace和业务实现方法名称

        opAddEntry = new QName("http://ws.yss.com", "helloWorld");

        //显示返回数据
        System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{"超人"}, classes)[0]);
    }
   
   
}





运行看是否执行成功。。。

你可能感兴趣的:(AXIS2入门示例)