Axis 安装 到http://ws.apache.org/axis/ 网站下在axix1.4版本,在eclipse中新建web项目 new->project->dynamic web project 命名为axis将axis lib中的jar包copy到 项目中的WEB-INF 的lib目录下。 修改web.xml将以下代码加到web.xml中
AxisServlet
org.apache.axis.transport.http.AxisServlet
AxisServlet
/servlet/AxisServlet
AxisServlet
*.jws
AxisServlet
/services/*
启动tomcat 运行http://localhost:8080/axis/servlet/AxisServlet 如果看到两个service(AdminService,Version)则说明axis安装成功 添加自己的Service 在项目中新建package com.test.service 并新建Class HelloWorld public class HelloWorld { public HelloWorld(){ } public String hello(String str){ return “hello ”+str; } } 编写deploy.wsdd文件 把Axis中的jar包添加到环境变量的classpath中,打开CMD找到wsdd所在的目录 输入如下命令java org.apache.axis.client.AdminClient deploy .wsdd 如果失败 检查class path 设置, 如果成功则运行http://localhost:8080/axis/servlet/AxisServlet 时会看到HelloWorld这个service 测试, 编写如下代码: public class TestWebService { public static void main(String[] args) throws ServiceException,MalformedURLException,RemoteException{ String endpoint = "http://localhost:8080/axis/services/HelloWorld"; Service service = new Service(); Call call = (Call)service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName("hello"); String res = (String) call.invoke(new Object[] {"test" }); System.out.println(res); } } 如果出现 hello test 则说明调用成功 传输自定义Object 创建传递的对象: package com.test.service; public class Response{ private String sessionId; private boolean success; private String ipAddress; public String getSessionId() { return sessionId; } public void setSessionId(String sessionId) { this.sessionId = sessionId; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getIpAddress() { return ipAddress; } public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; } } 修改service方法 package com.wind.service; public class HelloWorld { public HelloWorld(){ } public Response hello(String str){ Response response = new Response(); response.setSessionId("2222211"); response.setSuccess(true); response.setIpAddress("121"); return response; } } 因为webservice中默认传递的对象为基本对象,所以如果要传递自定义的对象要在发布的时候配置, 修改deploy.wsdd如下 添加了beanMapping这个tag,其中qname中的myNS为任意指定的值 Response要和 class的名字一致, xmlns的namespace 要和qname中的一致, 内容则任意 laungua geSpecificType属性则为要传输的对象的全路经 Undeploy 之前发布的service 新建 undeploy.wsdd 运行命令java org.apache.axis.client.AdminClient undeploy.wsdd 然后再运行java org.apache.axis.client.AdminClient deploy.wsdd 再去运行http://localhost:8080/axis/servlet/AxisServlet 如果看到HelloWorld 则发布成功 修改测试类 public class TestWebService { public static void main(String[] args) throws ServiceException,RemoteException{ String endpoint = "http://localhost:8080/axis/services/HelloWorld"; Service service = new Service(); Call call = (Call)service.createCall(); QName qn = new QName("urn:HelloWorld","Response" ); call.registerTypeMapping(Response.class,qn,new org.apache.axis.encoding.ser.BeanSerializerFactory(Response.class,qn),new org.apache.axis.encoding.ser.BeanDeserializerFactory(Response.class,qn)); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName("HelloWorld","hello")); Response res = (Response) call.invoke(new Object[] {"test" }); System.out.println(res.getIpAddress()); } } 如果打印出设置的值则说明成功了
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|