webService-AXis2

首先使用AXis2 实现webService 需要两个资源:

1.myEclipse 6.5 实现服务器端的插件  http://download.csdn.net/detail/u014104269/9027031
2. axis2.war :用于将WebService发布到Web容器中。http://download.csdn.net/detail/u014104269/9028309

步骤一:

1.中的两个插件,都是已经修改过名字的,如果没有,请将下载的axis2-eclipse-codegen-wizard.zip 和 axis2-eclipse-service-archiver-wizard.zip  压缩文件里的Axis2_Codegen_Wizard和 Axis_Service_Archiver-wizard文件夹,解压缩到C:Program   FilesMyEclipse 6.5eclipseplugins目录下。 

将刚刚解压缩的Axis2_Codegen_Wizard文件夹重命名为 org.apache.axis2.tool.codegen.eclipse.plugin.CodegenWizardPlugin , 而Axis_Service_Archiver-wizard文件夹重命名为 

org.apache.axis2.tool.Axis_Service_Archiver_1.3.0 在C:Program FilesMyEclipse 6.5eclipselinks目录下新建文件名  为:axis-eclipse-plugin.link  内容为: path=C:/Program Files/MyEclipse 6.5/eclipse/plugins   重新启动myeclipse,在file->new->other中可看到Axis2 Wizards,axis2插件安装成功。

(本例使用myEclipse 6.5 开发,如果更高版本的话,配置插件更容易)

步骤二:

将2.中的axis2放到Tomcat下的webapps ,启动服务,axis2.war会解压。

 接下来可以配置自己的项目了:

创建项目-

package ws;

public class TestWs {
    public String showName(String name) {
        return name;
    }
    public String getName() {
        return "cys";
    }
}

然后 Export ,将项目导出为text.jar,然后在项目上右键,找到New-》other-》Axis2 Service Archiver 将项目导出生成 .aar

文件 ,然后将该.aar 文件放在Tomcat下 Tomcat 5.5webappsaxis2WEB-INFservices 目录中。

其余配置较麻烦。容易出错,给一个我已经弄好的Helloworld  http://download.csdn.net/detail/u014104269/9028511

启动服务:

    1.http://localhost:8080/axis2/services/HelloService/getName 
    2.http://localhost:8080/axis2/services/HelloService/showName?name=cys

  就可以了。

  还可以使用.class 文件发布,只需要把编译好的.class 文件放在Tomcat下 Tomcat 5.5webappsaxis2WEB-INFpojo 目录中。

如果没有pojo目录自己创建一下就好了。

下面是我的 client端代码:

package ws;

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 RPCClient
{
    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://localhost:8081/axis2/services/MyService");
        options.setTo(targetEPR);
        //  指定getGreeting方法的参数值
        Object[] opAddEntryArgs = new Object[] {"超人"};
        //  指定getGreeting方法返回值的数据类型的Class对象
        Class[] classes = new Class[] {String.class};
        //  指定要调用的getGreeting方法及WSDL文件的命名空间
        QName opAddEntry = new QName("http://ws", "showName");
        //  调用getGreeting方法并输出该方法的返回值
        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
        //  下面是调用getPrice方法的代码,这些代码与调用getGreeting方法的代码类似
        classes = new Class[] {String.class};
        opAddEntry = new QName("http://ws", "getName");
        System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes)[0]);
        
        /*在本例中使用了RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。
         * invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;
         * 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
         * 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
         * 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
         * */
    } 
}

 

原文地址:https://www.cnblogs.com/caoyusongnet/p/AXIS2.html