Eclipse利用Axis2插件构建Web Service并测试

      在学习Web Service的时候,从网上找到前辈的博客http://www.cnblogs.com/hexinlin/p/3358558.html,并依此文的方法按部就班:编写欲发布的java类HelloDemo.java -> 生成.arr文件并发布服务至Tomcat\webapps\axis2\WEB-INF\services\下,且访问http://127.0.0.1:8080/axis2/services/HelloDemo?wsdl成功…但当行至文中步骤“c.生成stub类”,在Axis Codgen wizard向导WSDL file location一栏中填入“http://127.0.0.1:8080/axis2/services/HelloDemo?wsdl”时,报错:“Specified WSDL is invalid!, Please select a validated *.wsdl/*.xml file on previous page.”

package com.hxl.webservice.service;

public class HelloDemo {

    public String sayHello(String name) {
        return "Hello"+name;
    }
    
    public String getResp() {
        return "请求被响应";
    }
}

   一时没找到解决办法,只能另辟蹊径:用原先的HelloDemo.java生成一个WSDL文件HelloDemo.wsdl存在本地——D:\Project\TestWebSer\src\HelloDemo.wsdl。

然后利用HelloDemo.wsdl文件再生成相关的java代码。如下:

ExtensionMapper.java
GetResp.java
GetRespResponse.java
HelloDemoCallbackHandler.java
HelloDemoStub.java
SayHello.java
SayHelloResponse.java
TestMethods.java

      在HelloDemoStub.java中“implements HelloDemo”处报错。这是由于HelloDemo.java中的HelloDemo类并非接口interface。只需将“implements HelloDemo”删除即可。

public class HelloDemoStub extends org.apache.axis2.client.Stub implements HelloDemo{
protected org.apache.axis2.description.AxisOperation[] _operations;

       然后要做的是实现抽象类HelloDemoCallbackHandler中的receiveResultgetResp和receiveResultsayHello方法,以此来得到你想要的东西:

package com.hxl.webservice.stub;

import com.hxl.webservice.service.HelloDemoCallbackHandler;
import com.hxl.webservice.service.SayHelloResponse;
import com.hxl.webservice.service.GetRespResponse;

public class MyCallBack extends HelloDemoCallbackHandler {

    @Override
    public void receiveResultgetResp(GetRespResponse result) {
        System.out.println(result.getLocal_return());
    }

    @Override
    public void receiveResultsayHello(SayHelloResponse result) {
        System.out.println(result.getLocal_return());
    }
}

  然后再写一个方法集中调用的类TestMethods :

package com.hxl.webservice.service;
import com.hxl.webservice.stub.*;

import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;

public class TestMethods {
	public void testGetResp() throws Exception{
		HelloDemoStub stub = new HelloDemoStub();		
		GetResp gr = new GetResp();
		System.out.println(stub.getResp(gr).get_return());		
	}	
	public void testSayHello() throws Exception{
		
		HelloDemoStub stub  = new HelloDemoStub();
		SayHello sh = new SayHello();
		sh.setName(" hxl");
		System.out.println(stub.sayHello(sh).get_return());	
	}	
    public void testGetAyn() throws Exception {    	
    HelloDemoStub stub = new HelloDemoStub();
	GetResp gr = new GetResp();
	stub.startgetResp(gr ,new MyCallBack());
	
	SayHello sh = new SayHello();
	sh.setName(" Darren!");
	stub.startsayHello(sh, new MyCallBack());
	
	System.out.println("异步调用");
	System.in.read();	
	}	
}

  最后,写一个含main方法的测试类Clnt:

package com.hxl.webservice.service;

import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;

public class Clnt {
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {				

		TestMethods tm = new TestMethods();
		//tm.testGetResp();
		//tm.testSayHello();
		tm.testGetAyn();
	}
}

   执行打印结果:

        异步调用

        请求被响应

       Hello Darren!

 

posted @ 2015-12-09 17:42  RedGuardian  阅读(518)  评论(0编辑  收藏  举报