zoukankan      html  css  js  c++  java
  • 使用Axis2以普通的Java类建立Web Services

      Apache Axis2是一个Web Services系统服务和客户端的实现。

      1、下载和部署Axis2。

        1)下载地址:http://axis.apache.org/axis2/java/core/download.cgi

        下载Binary Distribution和WAR Distribution这两个压缩包。

        2)将下载的axis2-1.6.2-war.zip解压,解压后将axis2.war复制到tomcat的webapps目录下

        3)启动tomcat,浏览器访问:http://localhost:8080/axis2/ 出现以下界面,部署成功。

      

      2、以普通的Java类建立Web Services

        1)需要注意的:这个普通的Java类是没有package的

        2)在\apache-tomcat-6.0.35\webapps\axis2\WEB-INF\目录下建立pojo目录。

        3)Java类。

    /**
     * 第一个Web Services 
     * @author Luxh
     */
    public class FirstService {
    	
    	public String sayHello(String name) {
    		String result = "Hello,"+name+",this is first Axis2 Application!";
    		return result;
    	}
    	
    	public String getInfo() {
    		return "This is first Axis2 Application!";
    	}
    	
    }
    

        将这个类编译后的FirstService.class文件复制到\apache-tomcat-6.0.35\webapps\axis2\WEB-INF\pojo目录下,无需重启tomcat,Axis2支持热部署。

      4)访问:http://localhost:8080/axis2/services/listServices

        可以看到普通的Java类已经发布为web Services

       5)访问web Services

        http://localhost:8080/axis2/services/FirstService/getInfo

        其中 http://localhost:8080/axis2/services/FirstService/  是Web Services 的地址,getInfo是方法名称

      http://localhost:8080/axis2/services/FirstService/sayHello?name=LiHuai

      其中 http://localhost:8080/axis2/services/FirstService/  是Web Services 的地址,sayHello是方法名称,?name=LiHuai是参数名称和参数值

      传参数的时候要注意:一定要跟提供服务的方法中的参数名一致public String sayHello(String name)

      3、Java客户端使用RPC方式调用Web Services

        1)新建一个Java Project,将axis2-1.6.2-bin.zip解压,解压后将\axis2-1.6.2\lib\目录下的所有jar引入到项目的classpath路径中

        2)客户端代码

    package cn.luxh.ws.client;
    
    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 org.junit.Test;
    
    public class FirstClient {
    	
    	/**
    	 * 用RPC方法调用Web Services
    	 * @throws AxisFault 
    	 */
    	@Test
    	public void testGetServices() throws AxisFault {
    		
    		//创建一个访问服务的客户端
    		RPCServiceClient client = new RPCServiceClient();
    		//获取操作客户端选项的持有者
    		Options options = client.getOptions();
    		
    		//设置要调用的Web Services 的地址
    		String ServiceEPR = "http://localhost:8080/axis2/services/FirstService";
    		EndpointReference epr = new EndpointReference(ServiceEPR);
    		options.setTo(epr);
    		
    		//因为提供Web Services普通的Java类没有package,
    		//所以默认是http://ws.apache.org/axis2
    		//getInfo是要调用的方法
    		QName qName = new QName("http://ws.apache.org/axis2", "getInfo");
    		
    		//qName 			--调用的方法
    		//new Object[]{}	--传递的参数值
    		//new Class[]{}  	--返回值类型
    		Object[] result = client.invokeBlocking(qName, new Object[]{}, new Class[]{String.class});
    		System.out.println("返回的结果是:"+result[0]);
    		
    		//调用sayHello方法
    		qName = new QName("http://ws.apache.org/axis2", "sayHello");
    		result = client.invokeBlocking(qName, new Object[]{"LiHuai"}, new Class[]{String.class});
    		System.out.println("返回的结果是:"+result[0]);
    		
    
    	}
    }
    

      

      4、使用生成stub的方式调用Web Services

        可以使用wsdl2java命令生成调用Web Services的客户端代码

        wsdl2java命令格式:wsdl2java -uri http://localhost:8080/axis2/services/FirstService?wsdl -p cn.luxh.ws.client -s -o stub

        -uri 指定访问的Web Services wsdl文件路径

        -p 指定了生成的Java类的包名

        -o 指定了生成文件保存的根目录

        1)在命令行进入到axis2-1.6.2-bin.zip解压后的\axis2-1.6.2\bin\目录,因为wsdl2java命令在这个目录中

        2)输入 wsdl2java -uri http://localhost:8080/axis2/services/FirstService?wsdl -p client -s -o stub 然后回车

        3)执行完后,在\axis2-1.6.2\bin\目录下有个stub文件夹,在stub文件夹下的src\cn\luxh\ws\client包下有个FirstServiceStub.java和FirstServiceUnsupportedEncodingExceptionException.java类,我们把这两个类复制项目中相应的包下,就可以直接调用Web Services方法了。

        4)stub客户端代码

    package cn.luxh.ws.client;
    
    import java.rmi.RemoteException;
    
    import javax.xml.namespace.QName;
    
    import org.junit.Test;
    
    public class FirstClient {
    	
    	
    	@Test
    	public void testGetServiceByStub() throws RemoteException, FirstServiceUnsupportedEncodingExceptionException {
    		FirstServiceStub firstServceStub = new FirstServiceStub();
    		
    		//wsdl2java命令将Web Services 的方法封装成了静态类
    		//所以需要通过下面的方法获取返回结果
    		FirstServiceStub.GetInfo getInfo =  new FirstServiceStub.GetInfo();
    		String ruselt = firstServceStub.getInfo(getInfo).get_return();
    		System.out.println("getInfo方法返回值:"+ruselt);
    		
    		FirstServiceStub.SayHello sayHello = new FirstServiceStub.SayHello();
    		sayHello.setName("LiHuai");
    		ruselt = firstServceStub.sayHello(sayHello).get_return();
    		System.out.println("sayHello方法返回值:"+ruselt);
    		
    	}
    	
    }
    

      

  • 相关阅读:
    MT【235】两道函数题
    MT【234】正方形染色(二)
    MT【233】染色正方形
    MT【232】展开式中的系数
    MT【231】棋子方法数
    MT【230】一道代数不等式
    MT【229】最小值函数
    MT【228】整数解的个数
    Python-list中的排序
    IO多路复用
  • 原文地址:https://www.cnblogs.com/luxh/p/2726341.html
Copyright © 2011-2022 走看看