读书人

替你的项目加入webservice(axis)

发布时间: 2012-06-27 14:20:09 作者: rapoo

为你的项目加入webservice(axis)
?????? unpackWARs="true" autoDeploy="true"
?????? xmlValidation="false" xmlNamespaceAware="false">

后加入:

<Context docBase="E:/eclipseWork/testAxis/web" path="/testAxis" reloadable="true" />

启动tomcat 访问: http://localhost:8080/testAxis/services

可以看到:

And now... Some ServicesAdminService (wsdl) AdminService Version (wsdl) getVersion

?3、编写webservice服务端

在web项目下新建一个类

view plain
  1. package?com.neo.test;????
  2. public?class?HelloWorld?{???public?String?sayHello()?{??
  3. ??return?"hello";???}??
  4. }??

4、注册服务

在 E:/eclipseWork/testAxis/web/WEB-INF 下新建文本文件 deploy.wsdd

view plain
  1. <deployment?xmlns="http://xml.apache.org/axis/wsdd/"?xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">???<service?name="SayHello"?provider="java:RPC">??
  2. ??<parameter?name="className"?value="com.neo.test.HelloWorld"/>????<parameter?name="allowedMethods"?value="*"/>??
  3. ?</service>??</deployment>??

启动刚刚配置好的项目,并确保访问http://localhost:8080/testAxis/services页面显示正常

打开cmd

cd E:/eclipseWork/testAxis/web/WEB-INF


E:/eclipseWork/testAxis/web/WEB-INF>java -Djava.ext.dirs=E:/eclipseWork/testAxis/web/WEB-INF/lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/testAxis/servlet/AxisServlet deploy.wsdd

?

成功的话会显示:

Processing file deploy.wsdd
<Admin>Done processing</Admin>

并在 E:/eclipseWork/testAxis/web/WEB-INF 下面产生 server-config.wsdd 文件

重启tomcat,并访问 http://localhost:8080/testAxis/services?会发现多出来一个service

And now... Some ServicesAdminService (wsdl) AdminService Version (wsdl) getVersion SayHello (wsdl) sayHello

就说明你的配置成功了

?

5、测试webservice

访问 http://localhost:8082/testAxis/services/SayHello?wsdl

view plain
  1. <?xml?version="1.0"?encoding="UTF-8"?>??<wsdl:definitions?targetNamespace="http://localhost:8082/testAxis/services/SayHello"?xmlns:apachesoap="http://xml.apache.org/xml-soap"?xmlns:impl="http://localhost:8082/testAxis/services/SayHello"?xmlns:intf="http://localhost:8082/testAxis/services/SayHello"?xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"?xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"?xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"?xmlns:xsd="http://www.w3.org/2001/XMLSchema">??
  2. <!--WSDL?created?by?Apache?Axis?version:?1.4??Built?on?Apr?22,?2006?(06:55:48?PDT)-->??
  3. ?????<wsdl:message?name="sayHelloRequest">??
  4. ?????</wsdl:message>??
  5. ?????<wsdl:message?name="sayHelloResponse">??
  6. ????????<wsdl:part?name="sayHelloReturn"?type="xsd:string"/>??
  7. ?????</wsdl:message>??
  8. ?????<wsdl:portType?name="HelloWorld">??
  9. ????????<wsdl:operation?name="sayHello">??
  10. ???????????<wsdl:input?message="impl:sayHelloRequest"?name="sayHelloRequest"/>??
  11. ???????????<wsdl:output?message="impl:sayHelloResponse"?name="sayHelloResponse"/>??
  12. ????????</wsdl:operation>??
  13. ?????</wsdl:portType>??
  14. ?????<wsdl:binding?name="SayHelloSoapBinding"?type="impl:HelloWorld">??
  15. ????????<wsdlsoap:binding?style="rpc"?mce_style="rpc"?transport="http://schemas.xmlsoap.org/soap/http"/>??
  16. ????????<wsdl:operation?name="sayHello">??
  17. ???????????<wsdlsoap:operation?soapAction=""/>??
  18. ???????????<wsdl:input?name="sayHelloRequest">??
  19. ??????????????<wsdlsoap:body?encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"?namespace="http://test.neo.com"?use="encoded"/>??
  20. ???????????</wsdl:input>??
  21. ???????????<wsdl:output?name="sayHelloResponse">??
  22. ??????????????<wsdlsoap:body?encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"?namespace="http://localhost:8082/testAxis/services/SayHello"?use="encoded"/>??
  23. ???????????</wsdl:output>??
  24. ????????</wsdl:operation>??
  25. ?????</wsdl:binding>??
  26. ?????<wsdl:service?name="HelloWorldService">??
  27. ????????<wsdl:port?binding="impl:SayHelloSoapBinding"?name="SayHello">??
  28. ???????????<wsdlsoap:address?location="http://localhost:8082/testAxis/services/SayHello"/>??
  29. ????????</wsdl:port>??
  30. ?????</wsdl:service>??
  31. ??</wsdl:definitions>??

打开cmd

?

cd E:/eclipseWork/testAxis

?

E:/eclipseWork/testAxis>java -Djava.ext.dirs=E:/eclipseWork/testAxis/web/WEB-INF
/lib org.apache.axis.wsdl.WSDL2Java -oE:/eclipseWork/testAxis/src -pcom.neo.clie
nt http://localhost:8082/testAxis/services/SayHello?wsdl

?

执行后在src下产生 E:/eclipseWork/testAxis/src/com/neo/client 文件夹里面有四个java文件:

HelloWorld.java

HelloWorldService.java

HelloWorldServiceLocator.java

SayHelloSoapBindingStub.java

?

这是根据服务器端提供的wsdl生成的客户端需要的基础文件

?

在com.neo.client下新建一个类

view plain
  1. package?com.neo.client;????
  2. import?java.rmi.RemoteException;??import?javax.xml.rpc.ServiceException;??
  3. ??public?class?HelloWorldClient?{??
  4. ????public?void?testSayHello()?throws?ServiceException,?RemoteException{??????????HelloWorldService?service?=?new?HelloWorldServiceLocator();??
  5. ????????HelloWorld?client??=?service.getSayHello();??????????System.out.println(client.sayHello());??
  6. ????}????????
  7. ????public?static?void?main(String[]?args)?{??????????HelloWorldClient?client?=?new?HelloWorldClient();??
  8. ????????try?{??????????????client.testSayHello();??
  9. ????????}?catch?(RemoteException?e)?{??????????????e.printStackTrace();??
  10. ????????}?catch?(ServiceException?e)?{??????????????e.printStackTrace();??
  11. ????????}??????}??
  12. }??

直接运行,如果打印出 hello 就说明客户端调用成功!

?

转载:http://blog.csdn.net/nsrainbow/article/details/4131196

读书人网 >Web前端

热点推荐