一、Dynamic Proxy方式 (动态代理)
1、先下载axis-1.4.zip:http://download.csdn.net/detail/jiangqingqingqing/4476589。解压文把axis文件夹复制到Tomcat的webapps目录下面。还需要一些jar包:http://download.csdn.net/detail/jiangqingqingqing/4476520。
2、创建一个普通的Java Projoct,然后新建一个接口,该接口必须继承java.rmi.Remote。该接口将作为Proxy,代码如下:
import java.rmi.Remote;
public interface HelloWorldInterface extends Remote{
String getName(String name);
int add(int a,int b);
}
3、新建一个 class:HelloWorld(不一定得实现HelloWorldInterface接口,但建议最好实现),代码如下:
public class HelloWorld implements HelloWorldInterface{
public String getName(String name){
return "Hello ,"+name;
}
public int add(int a,int b){
return a+b;
}
}
4、将HelloWorld.java复制到刚才部署在tomcat的axis文件夹里面,并重名为HelloWorld.jws。然后重新启动tomcat,在浏览器中输入http://localhost:端口号/axis/HelloWorld.jws?wsdl,就会得到一个wsdl文件,表明服务发布成功!
5、调用服务
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.*;
public class Text {
public static void main(String[] args) throws ServiceException, MalformedURLException {
//服务的路径
String wsdlURL = "http://localhost:8080/axis/HelloWorld.jws?wsdl";
//命名空间,必须跟wsdl文件里的targetNamespace标签一致
String nameSpace = "http://localhost:8080/axis/HelloWorld.jws";
String serviceName = "HelloWorldService";//必须跟wsdl文件里面的“service” 标签的name属性一致
String portType = "HelloWorld";//必须跟wsdl文件里面的“portType” 标签的name属性一致
//创建一个服务工厂
ServiceFactory factory = ServiceFactory.newInstance();
//通过服务工厂获取一个服务实例:服务路径,服务的QName
Service service = factory.createService(new URL(wsdlURL),new QName(nameSpace,serviceName));
//创建代理
HelloWorldInterface h = ( HelloWorldInterface)service.getPort(new QName(nameSpace,portType), HelloWorldInterface.class);
//通过代理调用方法
System.out.println(h.getName("Bat_jiang")+" sum = "+h.add(1, 2));
}
}
6.输出结果
Hello ,Bat_jiang sum = 3
二、axis2 pojo
1、同样的,先下载axis2.war:http://download.csdn.net/detail/jiangqingqingqing/4476628,然后部署在tomcat上。还有一些jar包:http://download.csdn.net/detail/jiangqingqingqing/4476529
2、编写一个HelloWorld类(注意:类里不能有package 包名):
public class HelloWorld {
public String getName(String name){
return "Hello ,"+name;
}
public int add(int a,int b){
return a+b;
}
}
3、编译生成HelloWorld.class。然后复制到部署后的axis2\WEB-INF\pojo文件夹里(如果没有pojo文件夹则新建一个)。在浏览器里输入:http://localhost:端口号/axis2/services/listServices,则可以查看到服务列表:
Available servicesVersionService EPR : http://localhost:8080/axis2/services/VersionService Description : VersionService Status : ActiveAvailable Operations
TimeTestService EPR : http://localhost:8080/axis2/services/TimeTestService Description : No description available for this serviceService Status : ActiveAvailable Operations
HelloWorldService EPR : http://localhost:8080/axis2/services/HelloWorldService Description : No description available for this serviceService Status : ActiveAvailable Operations
| |||||||
|
4、调用服务
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import com.HelloWorld;
import com.TimeTest;
public class test {
public static void main(String[] args) throws AxisFault {
//命名空间,必须跟wsdl文件里的targetNamespace标签一致
String nameSpace = "http://ws.apache.org/axis2";
// EndpointReference的前缀,加上具体的类名则可以形成一个 EndpointReference对象
String endPointUrl = "http://localhost:8080/axis2/services";
//用RPC(远程过程调用协议)方法调用
RPCServiceClient service = new RPCServiceClient();
Options option = service.getOptions();
/*
* 服务1
*/
EndpointReference endpoint = new EndpointReference(endPointUrl+"/HelloWorld");
option.setTo(endpoint);
//service.invokeBlocking(QName,传入的参数(没有也要是new Object[]{}),返回的类型(没有也要是new Class[]{})
HelloWorld h = (HelloWorld)service.invokeBlocking(new QName(nameSpace,"add"), new Object[]{1,2},new Class[]{HelloWorld.class})[0];
//调用
System.out.println(h.add(1, 2));
System.out.println(h.getName("Bat_jiang"));
/*
* 服务2
*/
endpoint =new EndpointReference(endPointUrl+"/TimeTest");
option.setTo(endpoint);
//调用
TimeTest t = (TimeTest)service.invokeBlocking(new QName(nameSpace,"getDate"), new Object[]{},new Class[]{TimeTest.class})[0];
System.out.println(t.getDate());
}
}
5、运行结果
3
Hello ,Bat_jiang
2012-08-04 08:21:16