axis2 webService开发指南(3)

复杂对象类型的WebService

这次我们编写复杂点的WebService方法,返回的数据是我们定义属性带getter、setter方法JavaBean,一维数组、二维数组等

1、服务源代码

新建一个web project项目

代码如下:

package com.amy.service.imple;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import com.amy.model.MUser;

/**
 * 复杂类型的webservice实现
 *
 * @author zhujinrong
 *
 */
public class ComplexTypeService {

    /**
     * 上传文件
     *
     * @param b
     *            字节
     * @param len
     *            长度
     * @return 地址
     */
    public String upload4Byte(byte[] b, int len) {
        String path = "";
        FileOutputStream fos = null;
        try {
            String dir = System.getProperty("user.dir");
            System.out.println("user.dir:" + dir);
            File file = new File(dir + "/" + new Random().nextInt(100) + ".jsp");
            fos = new FileOutputStream(file);
            fos.write(b, 0, len);
            path = file.getAbsolutePath();
            System.out.println("File path: " + file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return path;
    }

    /**
     * 返回一维数组
     *
     * @param i
     *            数组的大小
     * @return 数组
     */
    public int[] getArray(int i) {
        int[] arr = new int[i];
        for (int j = 0; j < i; j++) {
            arr[j] = new Random().nextInt(1000);
        }
        return arr;
    }

    /**
     * 返回二维数组
     *
     * @return 二维数组
     */
    public String[][] getTwoArray() {
        return new String[][] { { "中国", "北京" }, { "日本", "东京" },
                { "中国", "上海", "南京" } };
    }

    /**
     * 返回一个用户的信息
     *
     * @return 用户信息
     */
    public MUser getUser() {
        MUser model = new MUser();
        try {
            model.setKeyID("234353463452343243534534");
            model.setName("amy");
            model.setEmail("[email protected]");
            model.setAddress("中国");
            System.out.println(model.toString());
        } catch (Exception ex) {
            ex.getStackTrace();
        }
        return model;
    }
}

MUser类

package com.amy.model;

import java.io.Serializable;

import net.sf.json.JSONObject;

public class MUser implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private String keyID;

    private String name;

    private String address;

    public String getKeyID() {
        return keyID;
    }

    public void setKeyID(String keyID) {
        this.keyID = keyID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    private String email;

    public String toString() {
        JSONObject object = JSONObject.fromObject(this);
        return object.toString();
    }
}

2 测试代码和结果

(1)获取用户信息

/**
     * 获取用户自定义对象
     *
     * @throws AxisFault
     */
    public static void GetUser() throws AxisFault {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        String address = "http://localhost:8080/axis2/services/WebServiceArray1";
        EndpointReference epr = new EndpointReference(address);
        options.setTo(epr);
        QName qName = new QName("http://imple.service.amy.com", "getUser");
        Object[] result = client.invokeBlocking(qName, new Object[] {},
                new Class[] { MUser.class });
        MUser user = (MUser) result[0];
        System.out.println("Muser:" + user.toString());
    }

(2)一维数组

/**
     * 一维数组测试
     *
     * @throws IOException
     */
    public static void getArray() throws IOException {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        String address = "http://localhost:8080/axis2/services/WebServiceArray1";
        EndpointReference epr = new EndpointReference(address);
        options.setTo(epr);
        QName qName = new QName("http://imple.service.amy.com", "getArray");
        Object[] result = client.invokeBlocking(qName, new Object[] { 3 },
                new Class[] { int[].class });
        int[] arr = (int[]) result[0];
        for (int i : arr) {
            System.out.println("int[]:" + i);
        }
    }

(3)二维数组

    /**
     * 获取二维数组
     *
     * @throws IOException
     */
    public static void getTwoArray() throws IOException {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        String address = "http://localhost:8080/axis2/services/WebServiceArray1";
        EndpointReference epr = new EndpointReference(address);
        options.setTo(epr);
        QName qName = new QName("http://imple.service.amy.com", "getTwoArray");
        Object[] result = client.invokeBlocking(qName, new Object[] {},
                new Class[] { String[][].class });
        String[][] arrStr = (String[][]) result[0];
        for (String[] s : arrStr) {
            for (String str : s) {
                System.out.println("String[][]" + str);
            }
        }
    }

(4)文件上传

/**
     * 上传文件
     *
     * @throws IOException
     */
    public static void upload4Byte() throws IOException {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        String address = "http://localhost:8080/axis2/services/WebServiceArray1";
        EndpointReference epr = new EndpointReference(address);
        options.setTo(epr);
        QName qName = new QName("http://imple.service.amy.com", "upload4Byte");

        String path = System.getProperty("user.dir");
        System.out.println("user.dir:" + path);
        File file = new File(path + "/WebRoot/index.jsp");
        FileInputStream fis = new FileInputStream(file);
        int len = (int) file.length();
        byte[] b = new byte[len];
        @SuppressWarnings("unused")
        int read = fis.read(b);
        fis.close();
        Object[] result = client.invokeBlocking(qName, new Object[] { b, len },
                new Class[] { String.class });
        System.out.println("upload:" + result[0]);
    }

查看上传后的文件

3 完整测试代码如下

package com.amy.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

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.amy.client.model.MUser;

/**
 * 调用webService复杂类型的服务
 *
 * @author zhujinrong
 *
 */
public class CallWebServiceArray {

    /**
     * 主函数
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
         // GetUser();
         //getArray();
         // getTwoArray();
        upload4Byte();
    }

    /**
     * 获取二维数组
     *
     * @throws IOException
     */
    public static void getTwoArray() throws IOException {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        String address = "http://localhost:8080/axis2/services/WebServiceArray1";
        EndpointReference epr = new EndpointReference(address);
        options.setTo(epr);
        QName qName = new QName("http://imple.service.amy.com", "getTwoArray");
        Object[] result = client.invokeBlocking(qName, new Object[] {},
                new Class[] { String[][].class });
        String[][] arrStr = (String[][]) result[0];
        for (String[] s : arrStr) {
            for (String str : s) {
                System.out.println("String[][]" + str);
            }
        }
    }

    /**
     * 一维数组测试
     *
     * @throws IOException
     */
    public static void getArray() throws IOException {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        String address = "http://localhost:8080/axis2/services/WebServiceArray1";
        EndpointReference epr = new EndpointReference(address);
        options.setTo(epr);
        QName qName = new QName("http://imple.service.amy.com", "getArray");
        Object[] result = client.invokeBlocking(qName, new Object[] { 3 },
                new Class[] { int[].class });
        int[] arr = (int[]) result[0];
        for (int i : arr) {
            System.out.println("int[]:" + i);
        }
    }

    /**
     * 上传文件
     *
     * @throws IOException
     */
    public static void upload4Byte() throws IOException {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        String address = "http://localhost:8080/axis2/services/WebServiceArray1";
        EndpointReference epr = new EndpointReference(address);
        options.setTo(epr);
        QName qName = new QName("http://imple.service.amy.com", "upload4Byte");

        String path = System.getProperty("user.dir");
        System.out.println("user.dir:" + path);
        File file = new File(path + "/WebRoot/index.jsp");
        FileInputStream fis = new FileInputStream(file);
        int len = (int) file.length();
        byte[] b = new byte[len];
        @SuppressWarnings("unused")
        int read = fis.read(b);
        fis.close();
        Object[] result = client.invokeBlocking(qName, new Object[] { b, len },
                new Class[] { String.class });
        System.out.println("upload:" + result[0]);
    }

    /**
     * 获取用户自定义对象
     *
     * @throws AxisFault
     */
    public static void GetUser() throws AxisFault {
        RPCServiceClient client = new RPCServiceClient();
        Options options = client.getOptions();
        String address = "http://localhost:8080/axis2/services/WebServiceArray1";
        EndpointReference epr = new EndpointReference(address);
        options.setTo(epr);
        QName qName = new QName("http://imple.service.amy.com", "getUser");
        Object[] result = client.invokeBlocking(qName, new Object[] {},
                new Class[] { MUser.class });
        MUser user = (MUser) result[0];
        System.out.println("Muser:" + user.toString());
    }
}

4 代码结构如下:

WebService服务

调用服务方

引用的jar包如下:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="com.genuitec.runtime.library/com.genuitec.generic_6.0">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="con" path="com.genuitec.runtime.library/com.genuitec.jstl_1.2.1">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="WEB-INF/lib"/>
            <attribute name="owner.project.facets" value="jst.web.jstl"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="lib/activation-1.1.jar"/>
    <classpathentry kind="lib" path="lib/axiom-api-1.2.13.jar"/>
    <classpathentry kind="lib" path="lib/axiom-impl-1.2.13.jar"/>
    <classpathentry kind="lib" path="lib/axis2-adb-1.6.2.jar"/>
    <classpathentry kind="lib" path="lib/axis2-adb-codegen-1.6.2.jar"/>
    <classpathentry kind="lib" path="lib/axis2-java2wsdl-1.6.2.jar"/>
    <classpathentry kind="lib" path="lib/axis2-kernel-1.6.2.jar"/>
    <classpathentry kind="lib" path="lib/axis2-transport-http-1.6.2.jar"/>
    <classpathentry kind="lib" path="lib/axis2-transport-local-1.6.2.jar"/>
    <classpathentry kind="lib" path="lib/commons-beanutils-1.7.0.jar"/>
    <classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/>
    <classpathentry kind="lib" path="lib/commons-collections-3.2.1.jar"/>
    <classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/>
    <classpathentry kind="lib" path="lib/commons-lang-2.3.jar"/>
    <classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
    <classpathentry kind="lib" path="lib/ezmorph-1.0.3.jar"/>
    <classpathentry kind="lib" path="lib/httpcore-4.0.jar"/>
    <classpathentry kind="lib" path="lib/json-lib-2.2.3-jdk15.jar"/>
    <classpathentry kind="lib" path="lib/mail-1.4.jar"/>
    <classpathentry kind="lib" path="lib/neethi-3.0.2.jar"/>
    <classpathentry kind="lib" path="lib/woden-api-1.0M9.jar"/>
    <classpathentry kind="lib" path="lib/woden-impl-commons-1.0M9.jar"/>
    <classpathentry kind="lib" path="lib/woden-impl-dom-1.0M9.jar"/>
    <classpathentry kind="lib" path="lib/wsdl4j-1.6.2.jar"/>
    <classpathentry kind="lib" path="lib/wstx-asl-3.2.9.jar"/>
    <classpathentry kind="lib" path="lib/xmlbeans-2.3.0.jar"/>
    <classpathentry kind="lib" path="lib/XmlSchema-1.4.7.jar"/>
    <classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
</classpath>

时间: 2024-10-14 15:04:31

axis2 webService开发指南(3)的相关文章

axis2 webService开发指南(1)

参考文件:blog.csdn.net/IBM_hoojo http://hoojo.cnblogs.com/ 1 WebService简介 WebService让一个程序可以透明的调用互联网的程序,不管具体的实现细节.只要WebService公开了服务接口,远程客户端就可以调用服务.WebService是基于Http的组件服务,Webservice是分散式应用程序的发展趋势. 1.2 WebService的开源实现 WebService更多是一种标准,而不是一种具体的技术.不同的平台,不同的语言

eclipse+axis2+webservice开发实例

1.参考文献: 1.利用Java编写简单的WebService实例  http://nopainnogain.iteye.com/blog/791525 2.Axis2与Eclipse整合开发Web Service  http://tech.ddvip.com/2009-05/1242968642120461.html 3.http://blog.csdn.net/lightao220/article/details/3489015 4.http://clq9761.iteye.com/blog

基于Myeclipse+Axis2的WebService开发实录

最近开始学习了下在Myeclipse开发工具下基于WebSerivce的开发,下面将相关相关关键信息予以记录 Myeclipse的安装,本文以Myeclipse2014-blue为开发环境,相关配置执行完善 从http://archive.apache.org/dist/ws/axis2/tools/下载Axis2包,下载 axis2-eclipse-codegen-wizard.zip,下载axis2-eclipse-service-archiver-wizard.zip 从http://ax

axis2+spring开发webservice服务器端

需求:开发VAC与SP间订购通知接口服务器端(SP端),给定VacSyncService_SPClient.wsdl文件 首先,官网下载axis2-1.6.2-bin.zip和axis2-1.6.2-war.zipaxis2-1.6.2-bin.zip包含axis2的jar包,工具和例子axis2-1.6.2-war.zip包含了axis2的web应用,发布web服务时,将自己的程序以特定文件结构发布到axis2的web应用的service目录中 1.根据wsdl生成服务器端代码解压axis2-

Webservice开发概念

一.Web Service基本概念 Web Service由两部分组成 SOAP--Web Service之间的基本通信协议. WSDL--Web Service描述语言,它定义了Web Service做什么,怎么做和查询的信息. 二.什么是 Webservice? Web 是使应用程序可以与平台和编程语言无关的方式进行相互通信的一项技术.Web 服务是一个软件接口,它描述了一组可以在网络上通过标准化的 XML 消息传递访问的操作.它使用基于 XML 语言的协议来描述要执行的操作或者要与另一个

Webservice开发流程

Webservice简单的介绍 Webservice开发使用的通信协议是SOAP,支持简单对象的访问 Webservice的发布方式很多,可以采用axis2.jdk1.6以上版本自带的jdk发布 Webservice开发大致流程: 自定义Webservice接口和对外提供的方法,需要注意的方法返回值类型,一般接口返回都是符合接口报文规范的报文, 但是需要考虑报文内容的大小对报文的反馈方式适当调整. 自定义Webservice接口的实现类,该类是真正需要发布为service的类,但是该类本身一般不

axis2 webservice入门知识(JS,Java,PHP调用实例源码)

背景简介 最近接触到一个银行接口的案子,临时需要用到axis2 webservice.自己现学现总结的一些东西,留给新手.少走弯路. Axis2简介 ①采用名为 AXIOM(AXIs Object Model)的新核心 XML 处理模型,利用新的XML解析器提供的灵活性按需构造对象模型. ②支持不同的消息交换模式.目前Axis2支持三种模式:In-Only.Robust-In和In-Out.In-Only消息交换模式只有SOAP请求,而不需要应答:Robust-In消息交换模式发送SOAP请求,

【资源共享】Rockchip I2C 开发指南 V1.0

2C设备的设备应用非常广泛,常见的包含重力传感器,触摸屏驱动芯片,音频解码等 这个文档是RK3399的I2C开发文档:<Rockchip I2C 开发指南 V1.0> 内容预览: 下载地址:http://developer.t-firefly.com/thread-12495-1-1.html

七日Python之路--第十二天(Django Web 开发指南)

<Django Web 开发指南>.貌似使用Django1.0版本,基本内容差不多,细读无妨.地址:http://www.jb51.net/books/76079.html (一)第一部分 入门 (1)内置数字工厂函数 int(12.34)会创建一个新的值为12的整数对象,而float(12)则会返回12.0. (2)其他序列操作符 连接(+),复制(*),以及检查是否是成员(in, not in) '**'.join('**')   或  '***%s***%d' % (str, int)