Axis official web site : http://axis.apache.org/
Axis2 Java: http://axis.apache.org/axis2/java/core/
-截止:2013-05-30 12:57:00 最新的Axis2版本为1.6.2,我使用的是1.6.0
1、下载axis2.
注:解压下载,找到bin目录下的axis2server.bat,双击启动axis2.
---准备需要用到的jar包:
activation-1.1.jar
axiom-api-1.2.11.jar
axiom-dom-1.2.11.jar
axiom-impl-1.2.11.jar
axis.jar
axis2-adb-1.6.0.jar
axis2-json-1.6.0.jar
axis2-kernel-1.6.0.jar
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
jaxrpc.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.9.jar
XmlSchema-1.4.7.jar
2、编写Java Bean
---创建一个工程名为mywebsercice,并编写Java类。
package com.boonya.webservice;
public class HelloWorld {
public String sayHello(String name) {
return "Hello, " + name + "\n";
}
}
3、编写服务配置
在再src下面建一个META-INF的文件夹,创建一个services.xml的文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- service (HelloWorld service name)-->
<service name="HelloWorld">
<Description>helloWorld example description </Description>
<!-- class -->
<parameter name="ServiceClass" locked="false">com.boonya.webservice.HelloWorld</parameter>
<!-- method -->
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCReceiver"/>
</operation>
</service>
4、部署webservice服务
把这个工程打包为jar文件,然后把扩展名jar改为aar,放到TomCat目录\webapp\axis2\WEB-INF\services的目录下面,启动tomcat服务。
5、查看服务
在浏览器输入:http://localhost:8080/axis2/services/,如果看到HelloWorld服务表示web服务部署成功
6、编写测试客户端
package com.boonya.webservice;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axiom.om.OMElement;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis2.AxisFault;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class HelloWorldClient
{
public void testMethod()
{
String method = "sayHello";
Service service = new Service();
Call call = null;
try
{
call = (Call) service.createCall();
try
{
call.setTargetEndpointAddress(new java.net.URL("http://localhost:8080/axis2/services/HelloWorld"));
} catch (MalformedURLException e)
{
e.printStackTrace();
}
call.setOperationName(new QName("http://com/boonya/webservice", method));
call.setUseSOAPAction(true);
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setSOAPActionURI("http://com/boonya/webservice/GetServerList");
String k = null;
try
{
k = (String) call.invoke(new Object[]{});
System.out.println(">>> " + k); // 返回值输出
} catch (RemoteException e)
{
e.printStackTrace();
} // 因为返回值是String类型,所以这里调用的返回值也是String类型
} catch (ServiceException e)
{
e.printStackTrace();
}
}
@SuppressWarnings(
{ "rawtypes" })
public void testMethodRpc() throws AxisFault
{
RPCServiceClient rpcClient = new RPCServiceClient();
Options opt = new Options(null);
opt.setTo(new org.apache.axis2.addressing.EndpointReference("http://localhost:8080/axis2/services/HelloWorld"));
opt.setAction("urn:sayHello"); // 方法
rpcClient.setOptions(opt);
OMElement element = rpcClient.invokeBlocking(new QName("http://com/boonya/webservice", "sayHello"), new Object[]{ null }); // null表示没有参数传递
Iterator values = element.getChildrenWithName(new QName("http://com/boonya/webservice", "return")); // return表示有返回值
while (values.hasNext())
{ // 遍历出获取的数据
OMElement omElement = (OMElement) values.next();
System.out.println(omElement.getText());
}
}
public static void main(String[] args) throws AxisFault
{
HelloWorldClient client=new HelloWorldClient();
client.testMethod();
client.testMethodRpc();
}
}
About resource from:http://www.360doc.com/content/08/0124/17/21290_1001075.shtml