1、在官方网站下载axis的工程(这个等下就有用的)和源码、jar包等,下载地址是:
2、解压下载的工程或源码,解压axis-bin-1.4可以看到大致目录是这样的:
docs是文档、lib是所需的jar包、sample是例子示例、xmls是当前工程所需的xml、webapps是当前工程的webroot目录;
我们打开webapps目录可以看到一个axis的文件夹,这个文件夹里面有WEB-INF文件夹和一些页面,将axis复制到你的tomcat的webapps目录下。然后启动tomcat服务,访问http://localhost:8080/axis/,看到如下下面页面信息证明部署成功了:
后续我们会用到次工程,用来部署一些简单例子,它将在我们的axis1.x的webService中发挥很大的作用!
3、这里我新建一个AxisWebService-one,创建好工程后,将刚才解压的axis-bin-1.4中的lib的jar包copy到当前工程的lib目录下,这样我们所需要的jar就导入完毕,jar如下图:
创建webService服务代码如下,代码如下:
package com.axis.service;
public class HelloWorldService {
public String sayHello(String name) {
return "Hello "+name;
}
}
4、复制HelloWorldService.class或者HelloWorldService.java到tomcat目录下的axis文件夹目录下即可;也就是tomcat下的webapps下的axis下即可,注意:还有重要的一点,一般就是要将这个java文件中的包名去掉,并且将这个文件后缀名重命名为jws格式;如果带包名的话,请求后编译的class将会在包路径下,这样我们在请求当前jws的时候就会出现找不到class,详细的你可以到发布在tomcat下的工程看看WEB-INF目录下的jwsClass就一目了然了。
上面的工作完成后,启动tomcat服务器,访问http://localhost:8080/axis/HelloWorldService.jws页面就会显示如下信息:
There is a Web Service here
然后点击下Click to see the WSDL就可以看到wsdl的xml文件里面的配置信息,内容如下:
分析下wsdl的xml文件内容:
targetNamespace=http://localhost:8080/axis/HelloWorldService.jws
就是我们访问的webService路径,显示wsdl的xml信息
<wsdl:message name="HelloWorldService">
<wsdl:part name="sayHelloReturn" type="xsd:string" />
</wsdl:message>
是返回值的信息,sayHelloResponse代表响应,即返回值,type是返回值的类型
<wsdl:message name="sayHelloRequest">
<wsdl:part name="name" type="xsd:string" />
</wsdl:message>
请求方法参数信息,sayHelloRequest即请求,part是参数parameter,type是参数的类型
<wsdl:portType name="HelloWorldService">
<wsdl:operation name="sayHello" parameterOrder="name">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest" />
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
portType的name是当前webService的名称,operation是一个操作,即可以调用的方法。name就是方法名称了,parameterOrder是参数,input输入即传入参数,output输出即返回的值;
<wsdl:service name="HelloWorldServiceService">
<wsdl:port binding="impl:HelloWorldServiceSoapBinding" name="HelloWorldService">
<wsdlsoap:address location="http://localhost:8080/axis/HelloWorldService.jws" />
</wsdl:port>
</wsdl:service>
webService的名称和绑定的信息,以及访问的url地址。
5、编写客户端代码代码来调用如下:
public static void main(String[] args) throwsRemoteException, MalformedURLException, ServiceException {
HelloWorld();
}
private static void HelloWorld() throws
ServiceException,RemoteException{
//生成服务对象Service
Service service=new Service();
Call call=(Call) service.createCall();
//设置Endpoint地址
call.setTargetEndpointAddress(
"http://localhost:8080/AxisWebService/HelloWorldService.jws");
//绑定请求方法名称
call.setOperationName("HelloWorld");
//通过call.invoke调用服务,获取返回值
String data=(String)call.invoke(new Object[]{"Leslie"});
System.out.println(data);
}
运行上面的代码就可以看到控制台输出: