1.Dynamic Invocation Interface ( DII)
2.Stubs方式
public class HelloService{
public String getName(String name)
{
return "Welcome to Axis, "+name;
}
}
2.将源码拷贝到AXIS_HOME下,重命名为 HelloService.jws
3.访问连接http://localhost:8080/axis/HelloService.jws?wsdl,页面显示axis自动生成的wsdl(并且在axis\WEB-INF\jwsClasses 生成HelloService.class类文件)
4.编写访问服务的客户端 HelloClient.java,编译,执行,在命令提示行下会看到 "return value is Welcome to Axis ,fengshan"
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/axis/HelloService.jws";
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName(
"http://localhost:8080/axis/HelloService.jws", "getName"));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
String ret = (String) call.invoke(new Object[] { "fengshan" });
System.out.println("return value is " + ret);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="HelloService" provider="java:RPC">
<parameter name="className"
value="HelloService"/>
<parameter name="allowedMethods" value="*"/> <!---这里也可以填写需要暴露的方法的名字->
</service>
</deployment>
首先,在环境变量中设置CLASSPATH,加入以下jar:%AXIS_LIB%\axis.jar;
%AXIS_LIB%\axis-ant.jar;
%AXIS_LIB%\commons-discovery.jar;
%AXIS_LIB%\commons-logging.jar;
%AXIS_LIB%\jaxrpc.jar;
%AXIS_LIB%\saaj.jar;
%AXIS_LIB%\log4j-
%AXIS_LIB%\wsdl4j.jar
可以通过以下命令查看是否设置好了CLASSPATH:Echo %CLASSPATH%。
Axis提供了实现WSDL2Java的命令行工具org.apache.axis.wsdl.WSDL2Java,利用这个工具可以从WSDL地址处生成Java代码。命令如下:(在axis目录下)
java org.apache.axis.wsdl.WSDL2Java -Nhttp://localhost:8080/axis/services/HelloService=com http://localhost:8080/axis/services/HelloService?wsdl
会在com(由上述命令行中参数设定)包下生成HelloService.java、HelloServiceService.java、HelloServiceServiceLocator.java和HelloServiceSoapBindingStub.java四个文件。
5.编写测试类(在axis目录下)
{
public static void main(String[] args)
{
try
{
com.HelloServiceServiceLocator locator=new com.HelloServiceServiceLocator();
com.HelloService service=locator.getHelloService();
System.out.println(service.getName("fengshan"));
}
catch(Exception e)
{
System.err.println("Execution failed.Exception:"+e);
}
}
}
public static void main(String[] args) {
try {
Service service = new Service();
String Url = "http://localhost:8080/axis/services/HelloService";
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(Url);
call.setOperationName(new QName(Url, "getName"));
String result = (String) call.invoke(new Object[] { "winwin" });
System.out.println(result);
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
其实上面四个自动生成的java文件也就是像以上这个客户端这样来调用的。可以对照看看。
















