1、在%TOMCAT_HOME%\webapps中放入axis2.war,这个可以在www.apache.org下载
2、启动Tomcat,将该war包解开
3、服务端代码
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* @company
*
* @description
*
* @author Terry.B.Li
*
* @date 2009.1.7
*/
public class WSServer {
public byte[] invoke(String id, byte[] bytes) {
byte[] result = null;
System.out.println("id:" + id);
Object[] params = byteConvertObj(bytes);
System.out.print("参数:");
for (Object param : params) {
System.out.println(param.getClass());
}
// 通过 Axis2 反射调用返回的对象
Object resultObj = "返回值";
//根据功能号 ID 解析调用方法
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(arrayOutputStream);
objectOutputStream.writeObject(resultObj);
result = arrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
private Object[] byteConvertObj(byte[] bytes) {
Object[] result = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectInputStream objectInputStream = null;
try {
byteArrayInputStream = new ByteArrayInputStream(bytes);
objectInputStream = new ObjectInputStream(byteArrayInputStream);
result = (Object[]) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return result;
}
}
4、将该代码编译后放入%TOMCAT_HOME%\webapps\axis2\WEB-INF\pojo下
5、客户端代码如下
package com.newegg.lab.ws.client;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class WSClient implements Serializable{
private static final long serialVersionUID = -8513162370253557533L;
private static final Log log = LogFactory.getLog(WSClient.class);
private static EndpointReference endpointReference = new EndpointReference("http://localhost:8080/axis2/services/WSServer");
private static String namespace = "http://ws.apache.org/axis2";
/**
* @param args
*/
public static void main(String[] args) {
try {
log.info(invoke("123456", "aaa", "得到的的", "埃担罚埃担罚",34,new WSClient()));
} catch (Exception e) {
e.printStackTrace();
}
log.info("OK");
}
private static Object invoke(String id,Object params){
Object result = null;
try {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
options.setTo(endpointReference);
QName qname = new QName(namespace,"invoke");
Class[] clz = new Class[]{byte[].class};
Object[] _params = new Object[]{id,objConvertByte((Object[])params)};
Object[] results = serviceClient.invokeBlocking(qname, _params, clz);
byte[] bytes = (byte[]) results[0];
result = byteConvertObj(bytes);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static byte[] objConvertByte(Object obj){
byte[] result = null;
ByteArrayOutputStream byteArrayOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
result = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
objectOutputStream.close();
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
private static Object byteConvertObj(byte[] bytes){
Object result = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectInputStream objectInputStream = null;
try {
byteArrayInputStream = new ByteArrayInputStream(bytes);
objectInputStream = new ObjectInputStream(byteArrayInputStream);
result = objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
6、将客户端代码打包放入%TOMCAT_HOME%\webapps\axis2\WEB-INF\lib下
7、启动Tomcat
8、测试访问:
http://localhost:8080/axis2/services/WSServer?wsdl
9、执行客户端代码