用Axis2实现Web Service,虽然可以将POJO类放在axis2/WEB-INF/pojo目录中直接发布成Web Service,这样做不需要进行任何配置,但这些POJO类不能在任何包中。这似乎有些不方便,为此,Axis2也允许将带包的POJO类发布成Web Service。
看网上的一些例子都比较简单,就是些打印之类的方法.看了些例子动手建了一个,下面把步骤贴上:
另附源码:http://download.csdn.net/detail/liweifengwf/5762875
一、新建一个web程序 webservice01
二、把官网的 axis2.war 中的 conf、modules、services、和lib中的jar包考到WEB-INF中
在services中新建文件夹 META-INF 中新建 service.xml
这个文件中写 关于服务类的配置
结构如下图:
services中的文件夹要和服务类相同,不知道为什么,这点理解的不够,肯定还有其他的方法。
三、修改web.xml文件,添加以下内容
AxisServlet
org.apache.axis2.transport.http.AxisServlet
1
AxisServlet
/services/*
四、实体类 User
package data;
import java.io.Serializable;
/**
* function: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 [email protected]
* @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
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return this.id + "#" + this.name + "#" + this.email + "#" + this.address;
}
}
五、建服务类:
package com.cn;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import data.User;
/**
* function:复杂类型数据的WebService:字节数组、返回一维int数组、二维String数组及自定义JavaBean对象等
*/
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("[email protected]");
user.setName("jack");
user.setId(22);
return user;
}
}
六、service.xml文件配置
ComlxexTypeService Service Example
com.cn.ComplexTypeService
其实operation元素不用每个方法都去配置的,也可以加上一个通用的
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 data.User;
/**
* function:复杂类型数据WebService客户端调用代码
*/
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);
}
}
八、重启tomcat 访问http://localhost:8080/axis2/services/ComplexTypeService?wsdl,看到wsdl页面说明配置成功。
运行客户端得到如下: