MyEclipse学习axis2 WebService学习笔记

一、配置环境

我用的版本是1.4.1。用到的软件和包

1.axis2-1.4.1-bin.zip

2.axis2-1.4.1-war.zip

MyEclipse安装axis2 插件我整理了资源,例子,架包,插件的下载地址点击打开链接

将axis2-1.4.1-war.zip 拷贝到tomcat webapps目录下 运行tomcat

在地址栏中输入http://localhost:8080/axis2 ,你会看到axis2的欢迎画面


二、构建服务

 1.建立要发布的Web Service

 (1) MyEclipse中建立一个 WebProject 命名为 TestService建立SimpleServer 类如下:

package com.deltaj.server;

public class SimpleServer {

	/** 
     * 简单的测试方法 
     *  
     */  
    public String simpleMethod(String name) {  
        return name + "Say this is a Simple method ^-^";  
    }  
}

主要的过程就是如何利用axis2的eclispe插件来发布这个服务啦。

1。在MyEclipse的packageExplorer 中点击右键,在菜单中选择新建--->other...----->Axis2Service Archiver

2.然后点击next进入了类选择页面,在这个页面中的Class File Location选择框中选择类所在的文件夹。 (到bin目录下就可以了)

3.点击next之后进入了选择 wsdl文件,这里我们选择skip wsdl。

4. 点击next之后,进入的是选择jar文件的页面,这里我们没有外部的jar,所以点击next直接跳过这个页面。

5.点击next之后,进入的是选择xml页面,这里我们选择的是自动生成xml,也就是勾选Generate the service xml automatically这一项

6.点击next之后,进入的是生成xml文件的页面,在service name 里填写这个服务所起的名字,这里我起名为simpleServer,然后在class name 中填写要发布的类,这里一定要 写全路径,写好后就可以点击load 按钮,

7 点击next后,进入的是输出artiver文件的页面,先要在outputFile location 中选择要输出的路径,在output  File Name中输入artiver文件的名称。我起的名字是simpleServer

8.点击finish,如果看到如下的画面,恭喜你,服务发布成功啦。

9.接下来,我们就可以把这个aar文件放入tomcat中发布,首先把生成的aar文件拷贝到tomcat目录中的axis2项目的service目录中位置如图。


10.接下来启动tomcat,在地址栏中输入 http://localhost:8080/axis2 ,你会看到axis2的欢迎画面
11.点击Service连接,你会看到发布的服务列表。这里面就能看到我们发布的simpleService
12.点击我们的服务simpleServer的连接, http://localhost:8080/axis2/services/simpleService?wsdl 。至此,服务发布成功。


三、编写客户端代码调用服务


(1) .用MyEclipse的axis2的插件来生成stub代码。

1.在eclispe 的packageExplorer 中点击右键,在菜单中选择新建--->other...----->Axis2Code Generator


2.点击next,进入下一个页面,选择从wsdl文件来产生java文件。

3. 点击next,然后选择wsdl文件,注意此处要填写上第二布发布成功的simpleServer链接

               http://localhost:8080/axis2/services/simpleService?wsdl

4.点击next,进入设置页面,这里我们就用默认的设置。

5. 点击next,选择输出文件的路径。我的路径是 桌面/TestClient

6.点击next,恭喜你已经生成代码成功。


7.MyEclipse里面建一个微博 web project 名为TestClient,然后inport 第五步的生成的TestClient ,然后导入axis2-1.4.1-bin中找到lib包,把其中的jar都加入我们的工程中

8.然后看到SimpleServerStub和SimpleServerCallbackHandler 就是自动生成的类

9.创建一个测试类

package com.deltaj.client;
import com.deltaj.server.SimpleServerStub;


public class SimpleClient {

	public static void main(String[] args) throws Exception{  
	    
		  //初始化桩文件  
		  SimpleServerStub stub = new SimpleServerStub();  
		  //初始化SimpleMethod方法。  
		  SimpleServerStub.SimpleMethod request = new  SimpleServerStub.SimpleMethod();  
		  //调用simpleMethod的setName方法。  
		  request.setName("zt");  
		  //  
		  System.out.println(stub.simpleMethod(request).get_return());  
		    
		    
		 }  
}

如果一切正常,你就会看到结果

log4j:WARN No appenders could be found for logger(org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
ztSay this is a Simple method ^-^。



上面我们做了一个简单的例子,调用的服务里的方法只是简单返回一个字符串,这次我们要做一个复杂点的例子,调用服务里的方法返回一个java Bean对象。

1. 首先做一个简单的java bean ,代码如下

package com.deltaj.client;

public class Persion {

	 // 姓名  
    private String name;  
    // 年龄  
    private int age;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public int getAge() {  
        return age;  
    }  
  
    public void setAge(int age) {  
        this.age = age;  
    }  
}

2.做一个服务,里面只发布一个方面,这个方法返回这个bean。

package com.deltaj.server;

import com.deltaj.client.Persion;

public class BeanServer {

	/** 
     * 简单的测试方法 
     *  
     */  
    public Persion testMethod(String name, int age) {  
        Persion persion = new Persion();  
        persion.setName(name);  
        persion.setAge(age);  
        return persion;  
    }  
}

 3.按照第二节的做法,把这个服务发布出来。

 4.同样按照第三节的方法,用这个wsdl来生成stub代码。

 5.然后我们开始做一个方法来调用一下这个服务。


package com.deltaj.client;

import com.deltaj.server.BeanServerStub;
import com.deltaj.server.BeanServerStub.Persion;

public class BeanClient {

	 public static void main(String[] args) throws Exception{  
	        // 初始化桩文件  
		 BeanServerStub stub = new BeanServerStub();  
	        // 初始化TestMethod方法。  
		 BeanServerStub.TestMethod request = new BeanServerStub.TestMethod();  
	        // 设置bean的属性值。  
	        request.setName("delta ");  
	        request.setAge(20);  
	        Persion p = stub.testMethod(request).get_return();
	        //     
	        System.out.println("The name is:"+p.getName());  
	        System.out.println("The age is:"+p.getAge());  
	 }  
}

6.如果按照这里说的做的话,你会看到

log4j:WARN No appenders could be found for logger(org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
The name is:delta
The age is:20



2.本例子的客户端不采用stub方式,而是一种实现通用的调用方式,不需要任何客户端存根即可访问服务。只需要指定对于的web servce地址、操作名、参数和函数返回类型即可。

package com.deltaj.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;

/**
 * 编写客户端代码调用服务
 * @author yeshun
 *
 */
public class WsClient {

	private RPCServiceClient serviceClient;
	private Options options;
	private EndpointReference targetEPR;

	public WsClient(String endpoint) throws AxisFault {
	   serviceClient = new RPCServiceClient();
	   options = serviceClient.getOptions();
	   targetEPR = new EndpointReference(endpoint);
	   options.setTo(targetEPR);
	}

	public Object[] invokeOp(String targetNamespace, String opName,
	    Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
	    ClassNotFoundException {
	   // 设定操作的名称
	   QName opQName = new QName(targetNamespace, opName);
	   // 设定返回值

	   // Class<?>[] opReturn = new Class[] { opReturnType };

	   // 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
	   return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
	}

	/**
	* @param args
	* @throws AxisFault
	* @throws ClassNotFoundException
	*/
	public static void main(String[] args) throws AxisFault,
	    ClassNotFoundException {
	   // TODO Auto-generated method stub
	   final String endPointReference = "http://localhost:8080/axis2/services/beanServer?wsdl";
	   // beanServer的包名(server.deltaj.com)
	   final String targetNamespace = "http://server.deltaj.com";
	   WsClient client = new WsClient(endPointReference);

	   String opName = "testMethod";
	   Object[] opArgs = new Object[] { "name",20 };
	   
	   //如果返回的是一个JavaBean 那2个项目的JavaBean package包必须一样com.deltaj.client
	   Class<?>[] opReturnType = new Class[] { com.deltaj.client.Persion.class };

	   Object[] response = client.invokeOp(targetNamespace, opName, opArgs,
	     opReturnType);
	   com.deltaj.client.Persion p = (com.deltaj.client.Persion)response[0];
	   System.out.println(p.getName()+"--"+p.getAge());
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值