本来很简单的一个环境搭建问题足足困扰了我一周的时间,所以思来想去还是写一篇博文记录下来,以后就不用那么四处去找资料找例子了,实在是浪费时间
废话不多说
1 首先在MyEclipse下创建WEB PROJECT 名称为 WebService
2 导入需要的jar包 在 http://pan.baidu.com/s/1gdWzowr 然后用 http://pan.baidu.com/s/1bnOzNEv替换项目中的web.xml
3 访问 http://127.0.0.1:8080/WebService/services 看看是否成功访问到网页 如果是则环境基本搭建
4 创建服务器类
package com.demo.axis; public class HelloService { public String sayHello(){ return "hello"; } public String sayHelloToPerson(String name){ if(name == "" || name.equals("")){ name = "nobody"; } return "hello " + name; } }
5 创建deploy.wsdd
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="HelloServices" provider="java:RPC"> <parameter name="className" value="com.demo.axis.HelloService"/> //如果将类放在包下,value应写成packagename.className的形式 <parameter name="allowedMethods" value="*"/> </service> </deployment>
6 发布该服务器类
java -Djava.ext.dirs="F:\GSXM\workspace\.metadata\.me_tca t\webapps\WebService\WEB-INF\lib" org.apache.axis.client.AdminClient -l http://1 27.0.0.1:8080/WebService/services/AdminService F:\GSXM\workspace\.metadata\.me_ tcat\webapps\WebService\WEB-INF\classes\deploy.wsdd
7 访问 http://127.0.0.1:8080/WebService/services 看见
8 编写客户端访问
package com.demo.axis; import java.io.IOException; import java.net.MalformedURLException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class Client { private static final String HELLO_SERVICE_ENDPOINT = "http://127.0.0.1:8080/WebService/services/HelloServices"; public void callSayHello() { try { Service service = new Service(); Call call = (Call) service.createCall(); //传递url call.setTargetEndpointAddress(new java.net.URL(HELLO_SERVICE_ENDPOINT)); //调用方法名字 call.setOperationName(new QName("http://webservice.sinosoft.com/","sayHello")); call.setReturnType(org.apache.axis.Constants.XSD_STRING); try { //ret即执行方法之后得到的返回值,此处不用传参,故不用写字符串 String ret = (String) call.invoke(new Object[] {}); System.out.println("The return value is:" + ret); return; } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } } public void callSayHelloToPerson() { try { Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(HELLO_SERVICE_ENDPOINT)); call.setOperationName(new QName("http://webservice.sinosoft.com/","sayHelloToPerson")); call.addParameter("name", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN); call.setReturnType(org.apache.axis.Constants.XSD_STRING); try { //ret即执行方法之后得到的返回值,此处传了一个字符串做为参数,如果不写空,会报错 String ret = (String) call.invoke(new Object[] {"Seven"}); System.out.println("The return value is:" + ret); return; } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } } public static void main(String[] args){ Client c = new Client(); c.callSayHello(); c.callSayHelloToPerson(); } }
结果
The return value is:hello The return value is:hello Seven
时间: 2024-10-13 12:43:55