加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService测试案例

发布时间:2020-12-17 01:06:52 所属栏目:安全 来源:网络整理
导读:?在浏览器中输入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl,我们可以看到HelloWorldService对应的WSDL信息,阅读这个WSDL文档,我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。只要拿到这
?在浏览器中输入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl,我们可以看到HelloWorldService对应的WSDL信息,阅读这个WSDL文档,我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。只要拿到这个WSDL就可以开发相应的客户端调用程序了。?
???1)通过WSDL文件生成客户端调用程序?
?????? 首先我们通过http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl我们可以获得WSDL文件HelloWorldService.wsdl,并将其放在src目录下面,接着我们通过程序访问该WSDL文件,并调用需测试的方法。此时测试类WebServiceClientTest.java的内容如下所示:?
Java代码?
  1. package?test;??
  2. ??
  3. import?org.codehaus.xfire.client.Client;??
  4. import?org.springframework.core.io.ClassPathResource;??
  5. import?org.springframework.core.io.Resource;??
  6. import?webservice.HelloWorld;??
  7. /**?
  8. ?*Copyright2007GuangZhouAmigo.?
  9. ?*Allrightreserved.????
  10. ?*HelloWorld的webservice的测试类.?
  11. ?*@author<a?href="mailto:xiexingxing1121@126.com">AmigoXie</a>?
  12. ?*@version1.0?
  13. ?*Creationdate:2007-9-16-下午05:36:05?
  14. ?*/??
  15. public?class?WebServiceClientTest?{???
  16. ????HelloWorld?helloWorld?=?null;??
  17. ????static?void?main(String[]?args)?throws?Exception?{??
  18. ???????WebServiceClientTest?test?=?new?WebServiceClientTest();??
  19. ???????test.testClient();??
  20. ????}???
  21. ??????
  22. void?testClient()? ???????String?wsdl?=?"HelloWorldService.wsdl";?//对应的WSDL文件??
  23. ???????Resource?resource?=?new?ClassPathResource(wsdl);???
  24. ???????Client?client?=?new?Client(resource.getInputStream(),?null);?//根据WSDL创建客户实例??
  25. ?????????
  26. ???????Object[]?objArray?=?new?Object[1];??
  27. ???????objArray[0]?=?"kk";??
  28. ???????//调用特定的Web?Service方法??
  29. ???????Object[]?results?=?client.invoke("sayHelloWorld",?objArray);??
  30. ???????System.out.println("result:?"?+?results[0]);??
  31. ????}??
  32. }??


运行该类,可得到如下输出结果:?

result: hello,kk?

可看出运行结果正确。?

2)根据服务地址创建客户端调用程序?

???? 接着让我们来看一个根据服务地址创建客户端调用程序的例子。我们可以通过测试类来测试Web Service的正确性,下面让我们来看一个简单的测试类,首先我们在src/test目录建立WebServiceClientTest.java文件,并在src目录下建立客户端调用的Spring配置文件client.xml。在client.xml配置文件中我们定义了一个testWebService的bean,该bean访问wsdlDocumentUrl为http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl的WSDL。该xml文件的详细内容如下:?
Xml代码?
    <?xml?version="1.0"?encoding="UTF-8"?>??
  1. <!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"??
  2. ????"http://www.springframework.org/dtd/spring-beans.dtd">??
  3. <beans ????bean?id="testWebService"?class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" ???????property?name="serviceClass">????????????
  4. ???????????????value>webservice.HelloWorld</>?????????
  5. property>????????
  6. ????????property?name="wsdlDocumentUrl">???????????
  7. ??????????????>http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl ?????bean>??


在WebServiceClientTest.java文件中获得HelloWorld,并调用它的sayHelloWorld方法来完成测试,该类的详细内容如下所示:?

import?org.springframework.context.ApplicationContext;??
  • import?org.springframework.context.support.ClassPathXmlApplicationContext;??
  • void?main(String[]?args)?{??
  • void?testClient()?{??
  • ???????ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext(??
  • ??????????????"client.xml");??
  • ???????helloWorld?=?(HelloWorld)?ctx.getBean("testWebService");??
  • ???????System.out.println(helloWorld.sayHelloWorld("kk"));??
  • }??


  • 在启动webservice_helloworld工程的情况下,运行WebServiceClientTest类,可看到控制台包含如下信息:?

    ??? hello,kk?

    ??? 由此可看出调用Web Service成功。

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读