Axis1.4环境配置:
配置一些工具在命令行中的运行环境:
AXIS_CLASSPATH 的值就是下面贴出来的内容:
%AXIS_HOME%/lib/axis.jar; %AXIS_HOME%/lib/axis-ant.jar; %AXIS_HOME%/lib/commons-discovery-0.2.jar; %AXIS_HOME%/lib/commons-logging-1.0.4.jar; %AXIS_HOME%/lib/jaxrpc.jar; %AXIS_HOME%/lib/log4j-1.2.8.jar; %AXIS_HOME%/lib/wsdl4j-1.5.1.jar; %AXIS_HOME%/lib/activation-1.1.jar; %AXIS_HOME%/lib/mail-1.4.jar; %AXIS_HOME%/lib/resolver.jar; %AXIS_HOME%/lib/saaj.jar; %AXIS_HOME%/lib/serializer.jar; %AXIS_HOME%/lib/xercesImpl.jar; %AXIS_HOME%/lib/xml-apis.jar;
Bean类的代码:
package samples.userguide.example5; /** * This is a JavaBean which represents an order for some products. * * @author Glen Daniels ([email protected]) */ public class Order { /** Who's ordering */ private String customerName; /** Where do they live */ private String shippingAddress; /** Which items do we want */ private String itemCodes[]; /** And how many */ private int quantities[]; // Bean accessors public String getCustomerName() { return customerName; } public void setCustomerName(String name) { customerName = name; } public String getShippingAddress() { return shippingAddress; } public void setShippingAddress(String address) { shippingAddress = address; } public String[] getItemCodes() { return itemCodes; } public void setItemCodes(String[] items) { itemCodes = items; } public int[] getQuantities() { return quantities; } public void setQuantities(int[] quants) { quantities = quants; } }
服务端代码:
package samples.userguide.example5; public class ArrayService { public String processOrders(Order[] orders) { String s = ""; if (orders != null && orders.length > 0) { s += "订单数组中共有" + orders.length + "元素\n"; for (Order order : orders) { s += "名称:" + order.getCustomerName() + ",地址:" + order.getShippingAddress() + "\n"; } } return s; } }
布署的配置文件deploy.wsdd ,这个文件是存放在 %WEB_APP%/WEB-INF/deploy.wsdd :
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="OrdersProcessor" provider="java:RPC"> <parameter name="className" value="samples.userguide.example5.ArrayService"/> <parameter name="allowedMethods" value="*"/> <beanMapping qname="myNS:Order" xmlns:myNS="urn:BeanService" languageSpecificType="java:samples.userguide.example5.Order"/> <arrayMapping qname="myNS:OrderArray" xmlns:myNS="urn:ArrayService" languageSpecificType="java:samples.userguide.example5.Order[]" innerType="myNS:Order" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </service> </deployment>
其实配置文件这里只需要针对复杂类型(Order类)进行手动配置即可,而不需要对数组再次定义。
生成server-config.wsdd,假设我们的WEB项目目录为D:\workspace-java\axis1\web
那么我们生成server-config.wsdd的命令为:
D:\workspace-java\axis1\web\WEB-INF>java -classpath %CLASSPATH% org.apache.axis.client.AdminClient -l http://localhost:8080/axis/services/OrdersProcessor deploy.wsdd
启动TCPMON(监控程序)
启动监控控制台,命令如下:
D:\>java -classpath %CLASSPATH% org.apache.axis.utils.tcpmon 9999 localhost 8080
接下来,如果我们在客户端访问web service时使用9999端口时,它将通过tcpmon代理并转发到tomcat的8080端口上。使用tcpmon工具有一个好处是可以看到所有web service交互的信封(envelope)。
客户端程序代码:
package samples.userguide.example5;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class ArrayClient {
public static void main(String[] args) throws ServiceException,
RemoteException {
String url = "http://localhost:9999/axis/services/OrdersProcessor";
Order[] orders = new Order[5];
for (int i = 0; i < 5; i++) {
Order order = new Order();
order.setCustomerName("customer name " + i);
order.setShippingAddress("shipping address " + i);
orders[i] = order;
}
Service service = new Service();
Call call = (Call) service.createCall();
QName qn1 = new QName("urn:ArrayService", "OrderArray");
QName qn2 = new QName("urn:BeanService", "Order");
call.registerTypeMapping(Order.class, qn2,
new org.apache.axis.encoding.ser.BeanSerializerFactory(
Order.class, qn2),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(
Order.class, qn2));
call.setTargetEndpointAddress(url);
Object[] arg1 = new Object[] { orders };
call.setOperationName(new QName("OrdersProcessor", "processOrders"));
call.addParameter("arg0", XMLType.SOAP_ARRAY
, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
String result = (String) call.invoke(arg1);
System.out.println(result);
}
}
需要注意的是客户端要传入一个数组,因此需要使用数组的SOAP定义( XMLType.SOAP_ARRAY )。