1.项目开发中多模块交互一般采用接口相互调用,调用方式一般包括webservice和http,对于安全性要求较高的接口(比如外网)一般采用webservice,而对于项目开发内网接口编程二者均可,不过post的调用会比较方便。下面来简单介绍这两种方式,有什么不对的地方还请大神多多指教!!!
对于webservice接口编程,其最底层是使用SOAP协议,目前最常用封装好的工具有Axis和XFire,首先去Apache官方下载axis,我用的是axis2-1.2这个版本,最新是axis2-1.3的版本,把下载的war包放到tomcat的webapps目录,启动tomcat服务,在浏览器地址栏输入http://localhost:8080/axis2/(根据每个人的tomcat配置的不同,这个路径可能不同) ,如果出现Apache界面就说明ws服务没有问题了,下面写个简单测试:TeestAxis.java
package com;
public class TestAxis{
public String axis1() {
return "hello,world";
}
}
在再src下面建一个meta-inf的文件夹,创建一个services.xml的文件,文件内容如下:
<service name="AXIS">
<Description>
test Axis
</Description>
<parameter name="ServiceClass" locked="false">com.axis.TestAxis</parameter>
<operation name="axis1">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
在上面的xml描述中,com.Hello是指你建立的类名称,hw是对应的方法,Hello是对应的服务名称。
把这个工程打包为jar文件,然后把扩展名jar改为aar,放到TomCat目录\webapp\axis2\WEB-INF\services的目录下面,启动tomcat服务。
在地址栏输入:http://localhost:8080/axis2/services/listServices ,如果服务正常,在浏览器页面中会出现处于Active状态的Hello的服务名称
客户端调用
目前我用了2种调用方法
Client.java
package com;
import org.apache.axiom.om.OMElement;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Iterator;
public class Client {
//
public static void main(String[] args) throws RemoteException, ServiceException, MalformedURLException {
/* **************** 调用方法1 *********************** */
RPCServiceClient rpcClient = new RPCServiceClient();
Options opt = new Options();
opt.setTo(new EndpointReference("http://localhost:8080/axis2/services/AXIS"));
opt.setAction("urn:axis1");
rpcClient.setOptions(opt);
OMElement element = rpcClient.invokeBlocking(new QName("http://com.axis", "axis1"), new Object[]{null});
System.out.println(element);
System.out.println(element.getFirstElement().getText());
Iterator value = element.getChildElements();
while(value.hasNext()){
OMElement ome = (OMElement)value.next();