1、這次我們編寫復雜點的WebService方法,返回的數據是我們定義屬性帶getter、setter方法JavaBean,一維數組、二維數組等。
代碼
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.util.Random;
import
data.User;
/** * <b>function:</b>復雜類型數據的WebService:字節數組、返回一維int數組、二維String數組及自定義JavaBean對象等 * @author hoojo * @createDate 2011-1-13 下午03:34:52 * @file ComplexTypeService.java * @package * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
ComplexTypeService {
public
String upload4Byte(
byte
[] b,
int
len) {
String path = "
";
FileOutputStream fos =
null
;
try
{
String dir = System.getProperty("
user.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;
}
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;
}
public
String[][] getTwoArray() {
return
new
String[][] { { "
中國
", "
北京
" }, { "
日本
", "
東京
" }, { "
中國
", "
上海
", "
南京
" } };
}
public
User getUser() {
User user =
new
User();
user.setAddress("
china
");
user.setEmail("
jack@223.com
");
user.setName("
jack
");
user.setId(22);
return
user;
}
}
代碼
package
data;
import
java.io.Serializable;
/** * <b>function:</b>User Entity * @author hoojo * @createDate Dec 16, 2010 10:20:02 PM * @file User.java * @package com.hoo.entity * @project AxisWebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
User
implements
Serializable {
private
static
final
long
serialVersionUID = 677484458789332877L;
private
int
id;
private
String name;
private
String email;
private
String address;
//getter/setter
@Override
public
String toString() {
return
this
.id + "
#
" +
this
.name + "
#
" +
this
.email + "
#
" +
this
.address;
}
}
值得注意的是這個User對象的package是data,如果是其它的package,你就需要在tomcat目錄下的webapps中的axis2的WEB-INF目錄下創建一個data目錄,和你的User對象的目錄保持一致。否則你的WebService將會出現ClassNotFontException異常。然后重啟你的tomcat,雖然axis2支持熱部署。
代碼
package
com.hoo.service;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.IOException;
import
javax.xml.namespace.QName;
import
org.apache.axis2.addressing.EndpointReference;
import
org.apache.axis2.client.Options;
import
org.apache.axis2.rpc.client.RPCServiceClient;
import
com.hoo.entity.User;
/** * <b>function:</b>復雜類型數據WebService客戶端調用代碼 * @author hoojo * @createDate 2011-1-13 下午03:36:38 * @file ComplexTypeServiceClient.java * @package com.hoo.service * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
ComplexTypeServiceClient {
public
static
void
main(String[] args)
throws
IOException {
RPCServiceClient client =
new
RPCServiceClient();
Options options = client.getOptions();
String address = "
http://localhost:8080/axis2/services/ComplexTypeService
";
EndpointReference epr =
new
EndpointReference(address);
options.setTo(epr);
QName qname =
new
QName("
http://ws.apache.org/axis2
", "
upload4Byte
");
String path = System.getProperty("
user.dir
");
File file =
new
File(path + "
/WebRoot/index.jsp
");
FileInputStream fis =
new
FileInputStream(file);
int
len = (
int
) file.length();
byte
[] b =
new
byte
[len];
int
read = fis.read(b);
//System.out.println(read + "#" + len + "#" + new String(b));
fis.close();
Object[] result = client.invokeBlocking(qname,
new
Object[] { b, len },
new
Class[] { String.
class
});
System.out.println("
upload:
" + result[0]);
qname =
new
QName("
http://ws.apache.org/axis2
", "
getArray
");
result = client.invokeBlocking(qname,
new
Object[] { 3 },
new
Class[] {
int
[].
class
});
int
[] arr = (
int
[]) result[0];
for
(Integer i : arr) {
System.out.println("
int[] :
" + i);
}
qname =
new
QName("
http://ws.apache.org/axis2
", "
getTwoArray
");
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);
}
}
qname =
new
QName("
http://ws.apache.org/axis2
", "
getUser
");
result = client.invokeBlocking(qname,
new
Object[] {},
new
Class[] { User.
class
});
User user = (User) result[0];
System.out.println("
User:
" + user);
}
}