Axis2测试webservice server以及client

一、环境搭建

下载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应该是最常用的。还有?

时间: 2024-08-21 08:03:06

Axis2测试webservice server以及client的相关文章

TCP server和client的一些测试

一.TCP server和client测试   socket设置 测试项/测试情景 send recv 测             server block           client block                                                                                              

Axis2发布webservice(1)--0配置发布

Axis2发布webservice(1)--0配置发布webservice 一. 准备工作 1.下载axis2程序包:   http://axis.apache.org/axis2/java/core/download.cgi      下载时选择Binary Distribution版本的zip格式文件和WAR Distribution的zip格式文件,总共2个zip文件:      axis2-1.6.2-bin.zip:包含axis2是所有jar包,再编程时根据需要将解压后的lib文件夹下

Eclipse+Tomcat+Axis2配置webservice。

前言 VS2012安装慢的出翔,借时间总结一下前两天初次搭建webservice的过程 . 整个项目需求是在android 的client端实现一个浏览器的插件, 并且需要调用webservice的功能. webservice这边亦需要连接数据库(以后有时间再总结),同时因为我创建的是java项目,又要用一些C#的源码,所以再后期可能还需要一些整合的工作.这篇文章仅总结一下搭建webservice的过程. 期间确实遇到了一些瓶颈(包括axis2的arr文件对配置文件读取的限制) 安装eclips

使用axis2构建webservice

axis2是可以实现webservice的一个插件,使用这个插件可以发布webservice 1:可以使用这个插件来发布webservice,可以看网址:http://clq9761.iteye.com/blog/976029/======个人感觉一般不太适合我们做项目时候使用 2:我们这里将axis2的夹包引入到一个web工程里,运行这个web工程来发布webservice===== 这是我们常用的,比如要你写一个webservice客户端,你要怎么做呢?创建一个web工程,然后引入axis2

Axis2创建WebService实例

  一.Axis2的下载和安装 1.可从http://ws.apache.org/axis2/ 下载Axis2的最新版本:      可以下载如下两个zip包:      axis2-1.5.4-bin.zip      axis2-1.5.4-war.zip      其中 axis2-1.5.4-bin.zip文件中包含了Axis2中所有的jar文件,       axis2-1.5.4-war.zip文件用于将WebService发布到Web容器中. 2.将axis2-1.5.4-war.

Axis2发布webservice(4)&mdash;webservice的异步调用

一,发布一个webservice,代码如下 package com.hoo.service; public class AsynchronousService { public String execute() throws InterruptedException{ //让当前线程睡眠5钟,展示异步调用 Thread.sleep(5000); return "done"; } } 二.发布Service,参见前面教程,不多讲 三.RPC方式异步调用: import java.io.I

【webservice】发布axis2的webservice服务端

axis2版本:axis2-1.5.4 准备工作:下载axis2-1.5.4-war.zip(生成服务端).axis2-1.5.4-bin.zip(axis2的jar包),jdk5(及以上版本).tomcat(端口我设成8086了) 手把手超级详细介绍axis2的webservice服务端的生成与发布. 1. 解压axis2-1.5.4-war.zip得axis2.war,把axis2.war放到tomcat的webapps目录, 启动tomcat就能加载axis2.war并生成新的axis2目

用AXIS2发布WebService的方法(转)

Axis2+tomcat6.0 实现webService 服务端发布与客户端的调用. 第一步:首先要下载开发所需要的jar包 下载:axis2-1.6.1-war.zip http://www.apache.org/dist//axis/axis2/java/core/1.6.1/ 下载完后解压至tomcat安装目录下的webapps文件夹下,启动tomcat后,在webapps目录下会生成axis2文件夹. 访问http://localhost:8080/axis2/能看到以下页面表示axis

Python之测试webservice接口

前段时间学习了Python操作http接口,觉得挺容易的.最近项目组也有接触webservice接口,心里想想是否Python也可以操作这类接口.于是利用伟大的度娘,花了6个小时研究出来了,所以迫不及待更新一篇博文来分享.有兴趣讨论的同学可以加入我们的交流Q群:297669715. 一.准备环境 webservice接口测试,需要用到suds库,网上百度的各种suds库都没法安装,我这里的Python3.5版本,所以安装不了那些suds库也没有办法在线安装,所以这里就提供一个大家都可用的方法和s