使用Axis2以普通的Java类建立Web Services

使用Axis2以普通的Java类建立Web ServicesPosted on

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

  1、下载和部署Axis2。

    1)下载地址:

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

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

    3)启动tomcat,浏览器访问::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)访问::8080/axis2/services/listServices

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

  5)访问web Services

    :8080/axis2/services/FirstService/getInfo

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

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

  其中 :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*/@Testpublic 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,网站空间,//所以默认是//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 :8080/axis2/services/FirstService?wsdl -pcn.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 :8080/axis2/services/FirstService?wsdl -p client -s -o stub 然后回车

一个人,一条路,人在途中,心随景动,

使用Axis2以普通的Java类建立Web Services

相关文章:

你感兴趣的文章:

标签云: