复杂对象类型的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("2804163771@qq.com");
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>

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

  1. axis2 webService开发指南(1)

    参考文件:blog.csdn.net/IBM_hoojo http://hoojo.cnblogs.com/ 1 WebService简介 WebService让一个程序可以透明的调用互联网的程序,不 ...

  2. axis2 webService开发指南(2)

    1  Axis2的简单WebService示例 1.1 新建一个web工程,创建一个类Greeting,用于当作webservice服务 代码如下: package amyservices; impo ...

  3. eclipse+axis2+webservice开发实例

    myeclipse10安装axis2插件 第一步:下载axis2-1.6的插件压缩包,axis2-eclipse-codegen-plugin-1.6.2.zip 和 axis2-eclipse-se ...

  4. WebService开发指南

    WebServiceInAurora Web Service Web Service是一种面向服务的架构的技术,通过标准的Web协议提供服务,目的是保证不同平台的应用服务可以互操作.在Aurora框架 ...

  5. 基于Myeclipse+Axis2的WebService开发实录

    最近开始学习了下在Myeclipse开发工具下基于WebSerivce的开发,下面将相关相关关键信息予以记录 Myeclipse的安装,本文以Myeclipse2014-blue为开发环境,相关配置执 ...

  6. Axis2 webservice入门--写个简单的webservice

    上一篇介绍了webservice开发前的准备.下面开始写webservice.如果不了解axis2请看上一篇,如果是新手:建议一边看一边写代码,自己动手完成这个过程. 一.新建一个web项目 二.新建 ...

  7. axis1,xfire,jUnit 测试案列+开Web Service开发指南+axis1.jar下载 代码

    axis1,xfire,jUnit 测试案列+Web Service开发指南(中).pdf+axis1.jar下载    代码 项目和资源文档+jar 下载:http://download.csdn. ...

  8. Webservice开发概念

    一.Web Service基本概念 Web Service由两部分组成 SOAP--Web Service之间的基本通信协议. WSDL--Web Service描述语言,它定义了Web Servic ...

  9. ASP.NET Aries 开源开发框架:开发指南(一)

    前言: 上周开源了Aries开发框架后,好多朋友都Download了源码,在运行过程里,有一些共性的问题会问到. 所以本篇打算写一下简单的开发指南,照顾一下不是太看的懂源码的同学,同时也会讲解一下框架 ...

随机推荐

  1. python环境搭建-Linux系统下python2.7升级python3.5.2步骤

    首先Python 查看版本 , 在Linux下特别注意权限问题,创建目录时候切记给予权限 如果是 ubnutu 请使用首先切换到 sudo su , 否则 make install 会出现问题.. 升 ...

  2. flask第二十篇——模板【3】

    请关注公众号:自动化测试实战 现在我们通过查询字符串的方式给render_template传参,我们就要用到flask库的flask.request.args.get()函数先获取参数,在index. ...

  3. Windows下Redis的使用

    Redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括st ...

  4. 日志框架--(二)JDK Logging

    前言 从jdk1.4起,JDK开始自带一套日志系统.JDK Logger最大的优点就是不需要任何类库的支持,只要有Java的运行环境就可以使用.相对于其他的日志框架,JDK自带的日志可谓是鸡肋,无论易 ...

  5. IO流-文件夹的拷贝

    文件夹的拷贝操作 要求: 完成文件夹的拷贝,包括子目录的拷贝和所有文件的拷贝 分析: 首先,得在目标目录下创建一个与源文件夹名称相同的文件夹 遍历源文件夹中的所有文件对象,判断子文件是目录还是文件 如 ...

  6. 使用PHP类库PHPqrCode生成二维码

    PHPqrCode是一个PHP二维码生成类库,利用它可以轻松生成二维码,官网提供了下载和多个演示demo, 查看地址:http://phpqrcode.sourceforge.net/.    下载官 ...

  7. 可以方便配合 Git 的现代编辑器

    可以方便配合 Git 的现代编辑器 虽然有些人说编辑器不重要,有人用记事本来编辑 PHP(我是不推荐的),但学是要推荐一些现在编辑器. 可以很方便的配合 Git,比如有冲突时不会看得晕乎乎,使用鼠标点 ...

  8. Oracle12c之 CDB数据库中数据字典架构

    数据字典就是元数据的集合,比如创建的表,列,约束,触发器等等这些都是元数据,需要保存到数据库中.除此之外,Oracle自身的一些数据库对象,如目录,PL/SQL代码等等这些都是元数据,都需要存放在数据 ...

  9. kubernetes简单示例

    1. 安装 yum install -y etcd kubernetes 2. 启动 systemctl start etcd systemctl start docker systemctl sta ...

  10. 历届试题 Excel地址

    问题描述 Excel单元格的地址表示很有趣,它使用字母来表示列号. 比如, A表示第1列, B表示第2列, Z表示第26列, AA表示第27列, AB表示第28列, BA表示第53列, .... 当然 ...