Java 创建简单的WebService
使用Axis开发WebService
1.下载并安装Axis
1)登陆http://axis.apache.org/axis/站点下载Axis
下载axis-bin-1_4.zip,解压后的目录结构如下:
2)安装Axis
首先要成功的安装Tomcat,然后将下载的Axis目录下面的webapps中的axis的
整个文件夹全部拷贝到Tomcat安装目录的Webapps下面
启动Tomcat,在浏览器中输入http://localhost:8080/axis之后
显示的界面如下:
Axis安装完毕。
2.开发WebService
1)即时发布的WebService
即时发布提供了一种非常简单的发布方式,发布者只要有Java源代码,然后把其后缀名改成jws,拷贝到%Tomcat_HOME%\webapps\axis下即完成了发布工作。
a)服务端代码:文件名SayHelloServer.java
public class SayHelloServer {public String getName(String name){return "hello "+name;} }
package com.wl.webservice.test;import javax.xml.namespace.QName;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class SayHelloClient {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubtry {String endpoint="http://localhost:8080/axis/TestClient.jws";Service service=new Service();Call call=null;call=(Call)service.createCall();call.setOperationName(new QName(endpoint,"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 e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
package com.wl.webservice.test;public class HelloWorldWsdd {public String hello(String name){return "hello "+name;}}
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"><service name="HelloWorldWsdd" provider="java:RPC"><parameter name="className" value="com.wl.webservice.test.HelloWorldWsdd"/><parameter name="allowedMethods" value="hello"/></service></deployment>
package com.wl.webservice.test;import javax.xml.namespace.QName;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class HelloWorldWsddClient {public static void main(String[]args){try {String uri="http://localhost:8080/axis/services/HelloWorldWsdd";Service service=new Service();Call call=(Call)service.createCall();call.setTargetEndpointAddress(uri);call.setOperationName(new QName(uri,"hello"));String result=(String)call.invoke(new Object[]{"wanglei and zhangsan"});System.out.println("result="+result);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
<undeployment xmlns="http://xml.apache.org/axis/wsdd/"> <service name="HelloWorldWsdd" /></undeployment>