Axis2测试webservice server以及client

时间:2023-03-10 08:43:08

一、环境搭建

下载axis2-1.6.2-war.zip/axis2-1.6.2-bin.zip等。

参考axis2-1.6.2-war\README.txt以及axis2-1.6.2-war\axis2\WEB-INF\services\version-1.6.2\META-INF\services.xml的写法,可以通过编写webservice类并打包成jar的形式放在Tomcat容器的axis2 webapp下的services目录进行发布。

测试:把axis2目录拷贝到%TOMCAT_HOME%/webapps目录下面,启动tomcat,并访问http://localhost:8080/axis2,正常。

二、编写webservice server端

1 bean

package com.test.entity;

public class Student {

private String id;
    private String name;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + "]";
    }
}

2 webservice class

package com.test;

import com.test.entity.Student;

public class HelloWorld {

public String sayHello() {
        return "saying hellooooooooooooo----2----";
    }
    
    public Student query() {
        Student s = new Student();
        s.setId("1");
        s.setName("wsc123");
        return s;
    }
    
    public Integer addTowNumbers(int a, int b) {
        return a+b;
    }
}
3 services.xml

<service name="HelloWorld">
    <description>
        This service is to get the running Axis version
    </description>
    <parameter name="ServiceClass">com.test.HelloWorld</parameter>
    <operation name="sayHello">
        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
    <operation name="query">
        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
    <operation name="addTowNumbers">
        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
</service>

<!--
<service name="HelloWorld" scope="application">
    <description>
        POJO: HelloWorld Service
    </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.test.HelloWorld</parameter>
</service>
 -->

4 build.xml

<project name="helloworldservice" basedir="." default="deploy">

<property name="src.dir" value="src">
    </property>
    <property name="build.dir" value="${basedir}/build">
    </property>

<path id="build.classpath">
    </path>

<target name="init">
        <delete dir="${build.dir}">
        </delete>
        <mkdir dir="${build.dir}" />
        <mkdir dir="${build.dir}/classes" />
        <mkdir dir="${build.dir}/jar" />
    </target>

<target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${build.dir}\classes">
            <classpath refid="build.classpath">
            </classpath>
        </javac>
    </target>

<target name="makejar" depends="compile">
        <jar destfile="${build.dir}\jar\${ant.project.name}.jar">
            <fileset dir="${build.dir}/classes">
                <include name="**/*.class"/>
            </fileset>
            <metainf dir="${basedir}">
                <include name="services.xml"/>
            </metainf>
        </jar>
    </target>
    
    <target name="deploy" depends="makejar">
        <copy file="${build.dir}/jar/${ant.project.name}.jar" todir="D:\wsc\software\apache-tomcat-6.0.41\webapps\axis2\WEB-INF\services"></copy>
    </target>

</project>

5 readme.txt

通过POJO的方式发布一个WebService到tomcat容器中。
这里需要注意的是:
Ant打成的Jar的格式,META-INF目录下必须包括services.xml,
以及services.xml的写法,可以认为每个方法就是一对operation标签。。

三、编写RPC client端

1 需要引用axis2-1.6.2-war\axis2\WEB-INF\lib\*.jar

2 bean/entity

package com.test.entity;

public class Student {
    private String id;
    private String name;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + "]";
    }
}
3 rpc client类

package sample.addressbook.rpcclient;

import javax.xml.namespace.QName;

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

import com.test.entity.Student;

public class HelloWorldRPCClient {

public static void main(String[] args1) throws AxisFault {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/HelloWorld");
        options.setTo(targetEPR);
        // QName of the target method
        QName qName = new QName("http://test.com", "sayHello");
        // Constructing the arguments array for the method invocation
        Object[] opArgs = new Object[] {};
        Class[] returnTypes = new Class[] { String.class };
        // Invoking the method
        Object[] response = serviceClient.invokeBlocking(qName, opArgs, returnTypes);
        System.out.println(response[0]);
        
        //如何返回一个对象??
        //
        qName = new QName("http://test.com", "query");
        opArgs = new Object[]{};
        returnTypes = new Class[] {Student.class};
        response = serviceClient.invokeBlocking(qName, opArgs, returnTypes);
        System.out.println(response[0]);
        
        //如何传递参数到web service方法中?
        qName = new QName("http://test.com", "addTowNumbers");
        //opArgs = new Object[]{Integer.valueOf(3), Integer.valueOf(4)};
        opArgs = new Object[]{33, 44};
        returnTypes = new Class[]{Integer.class};
        response = serviceClient.invokeBlocking(qName, opArgs, returnTypes);
        System.out.println(response[0]);
        
        
    }
}

4 运行结果

log4j:WARN No appenders could be found for logger (org.apache.axis2.context.AbstractContext).
log4j:WARN Please initialize the log4j system properly.
saying hellooooooooooooo----2----
Student [id=1, name=wsc123]
77

5 readme.txt

参考Axis2的samples目录下的pojo示例
重点是sample.addressbook.rpcclient.HelloWorldRPCClient这个类,用于访问自己发布的HelloWorld这个WebService。

四、问题

客户端可以用多种形式,如adb访问?

Axis2支持多种协议,http应该是最常用的。还有?