一、
Axis
安装
1
、环境
J2SE SDK 1.3 or 1.4:
我使用
1.4.2 Servlet Container:
我使用的
Tomcat 5.0
3
、解压缩安装包,将
Axis_UNZIP_PATH/Axis-version/webapps
下的
Axis
包拷贝到
TOMCAT_HOME/webapps/
下,以下约定
Axis_HOME
为该
TOMCAT_HOME/webapps/Axis
目录
5
、以上步骤执行成功,可以开发
webservice
例子了
Axis
支持三种
web service
的部署和开发,分别为:
1
、
Dynamic Invocation Interface ( DII)
2
、
Stubs
方式
3
、
Dynamic Proxy
方式
二、编写
DII(Dynamic Invocation Interface )
方式
web
服务
1.
编写服务端程序
HelloClient
public class HelloClient
{
public String getName(String name)
{
return "hello "+name;
}
}
2
、将源码拷贝到
Axis_HOME
下,重命名为
HelloClient.jws
4
、编写访问服务的客户端
TestHelloClient.java
import org.apache.Axis.client.Call;
import org.apache.Axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
public class SayHelloClient2
{
public static void main(String[] args)
{
try
{
String endpoint =
"http://localhost:8080/Axis/HelloClient.jws";
{
public static void main(String[] args)
{
try
{
String endpoint =
"http://localhost:8080/Axis/HelloClient.jws";
Service service = new Service();
Call call = null;
Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName(
"http://localhost:8080/Axis/HelloClient.jws",
"getName"));
call.setTargetEndpointAddress
(new java.net.URL(endpoint));
"http://localhost:8080/Axis/HelloClient.jws",
"getName"));
call.setTargetEndpointAddress
(new java.net.URL(endpoint));
String ret = (String) call.invoke(new Object[]
{"zhangsan"});
System.out.println("return value is " + ret);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
{"zhangsan"});
System.out.println("return value is " + ret);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
三、编写
Dynamic Proxy
方式访问服务
1
、编写部署服务端程序,同上边
DII
方式,本次仍使用上边部署的
HelloClient
2
、编写代理接口
public interface HelloClientInterface
extends java.rmi.Remote
{
public String getName(String name)
throws java.rmi.RemoteException;
}
3
、编写并执行客户端程序
TestHelloClient.java
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
import javax.xml.namespace.QName;
public class TestHelloClient
{
public static void main(String[] args)
{
try
{
String wsdlUrl =
"http://localhost:8080/Axis/HelloClient.jws?wsdl";
String nameSpaceUri =
"http://localhost:8080/Axis/HelloClient.jws";
String serviceName = "HelloClientService";
String portName = "HelloClient";
{
public static void main(String[] args)
{
try
{
String wsdlUrl =
"http://localhost:8080/Axis/HelloClient.jws?wsdl";
String nameSpaceUri =
"http://localhost:8080/Axis/HelloClient.jws";
String serviceName = "HelloClientService";
String portName = "HelloClient";
ServiceFactory serviceFactory =
ServiceFactory.newInstance();
Service afService =
serviceFactory.createService(new URL(wsdlUrl),
new QName(nameSpaceUri, serviceName));
HelloClientInterface proxy = (HelloClientInterface)
afService.getPort(new QName(
nameSpaceUri, portName),
HelloClientInterface.class);
System.out.println
("return value is "+proxy.getName("john") ) ;
}catch(Exception ex)
{
ex.printStackTrace() ;
}
}
}
ServiceFactory.newInstance();
Service afService =
serviceFactory.createService(new URL(wsdlUrl),
new QName(nameSpaceUri, serviceName));
HelloClientInterface proxy = (HelloClientInterface)
afService.getPort(new QName(
nameSpaceUri, portName),
HelloClientInterface.class);
System.out.println
("return value is "+proxy.getName("john") ) ;
}catch(Exception ex)
{
ex.printStackTrace() ;
}
}
}
四、编写
wsdd
发布
web
服务,编写
stub client
访问
web
服务
1
、编写服务端程序
server,SayHello.java
,编译
server.SayHello.java
package server;
public class SayHello
{
public String getName(String name)
{
return "hello "+name;
}
}
2. 编写 LogHandler.java
import org.apache.Axis.AxisFault;
import org.apache.Axis.Handler;
import org.apache.Axis.MessageContext;
import org.apache.Axis.handlers.BasicHandler;
import java.util.Date;
public class LogHandler
extends BasicHandler
{
public void invoke
(MessageContext msgContext)
throws AxisFault
{
/** Log an access each time
we get invoked.
*/
try {
Handler serviceHandler
= msgContext.getService();
extends BasicHandler
{
public void invoke
(MessageContext msgContext)
throws AxisFault
{
/** Log an access each time
we get invoked.
*/
try {
Handler serviceHandler
= msgContext.getService();
Integer numAccesses =
(Integer)serviceHandler.getOption("accesses");
if (numAccesses == null)
numAccesses = new Integer(0);
numAccesses = new Integer
(numAccesses.intValue() + 1);
Date date = new Date();
String result =
date + ": service " +
msgContext.getTargetService() +
" accessed " + numAccesses + " time(s).";
serviceHandler.setOption
("accesses", numAccesses);
System.out.println(result);
} catch (Exception e)
{
throw AxisFault.makeFault(e);
}
}
}
(Integer)serviceHandler.getOption("accesses");
if (numAccesses == null)
numAccesses = new Integer(0);
numAccesses = new Integer
(numAccesses.intValue() + 1);
Date date = new Date();
String result =
date + ": service " +
msgContext.getTargetService() +
" accessed " + numAccesses + " time(s).";
serviceHandler.setOption
("accesses", numAccesses);
System.out.println(result);
} catch (Exception e)
{
throw AxisFault.makeFault(e);
}
}
}
3
、编写
wsdd
文件
deploy.wsdd
<deployment xmlns=
"http://xml.apache.org/Axis/wsdd/"
xmlns:java=
"http://xml.apache.org/Axis/wsdd/providers/java">
<handler name="print" type="java:LogHandler"/>
<service name="sayhello"
provider="java:RPC">
<requestFlow>
<handler type="print"/>
</requestFlow>
<parameter name="className"
value="server.SayHello"/>
<parameter name="allowedMethods"
value="*"/>
</service>
</deployment>
3
、将编译后的文件拷贝到
Axis_HOME/WEB-INF/classes
下,如:
D:/tomcat/webapps/Axis/WEB-INF/classes
4
、发布服务:
java org.apache.Axis.client.AdminClient deploy.wsdd
5
、生成
client stub
文件
a:
方式
1
将
SayHello.java
拷贝到
Axis_HOME/
下,重命名为
SayHello.jws
,
执行下面的命令生存
client stub
java org.apache.Axis.wsdl.WSDL2Java
-p client http://localhost:8080
/Axis/services/SayHello.jws?wsdl
b:
方式
2
执行如下命令生成
SayHello.wsdl
java org.apache.Axis.wsdl.Java2WSDL
-oSayHello.wsdl -lhttp://localhost:8080
/Axis/services/SayHello -nsayhello server.SayHello
执行如下命令生成
client stub
java org.apache.Axis.wsdl.WSDL2Java
SayHello.wsdl -p client
生成的
stub client
文件列表为:
1.SayHello.java
2.SayHelloService.java
。
3.SayHelloServiceLocator.java
4.SayHelloSoapBindingStub.java
6
、编写客户端程序,编译并执行
public class SayHelloClient
{
public static void main(String[] args)
{
try
{
SayHelloService service = new client.
SayHelloServiceLocator();
client.SayHello_PortType
client = service.getSayHello();
String retValue=client.getName("zhangsan");
System.out.println(retValue);
}
catch (Exception e)
{
System.err.println
("Execution failed. Exception: " + e);
}
}
}