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

webService

发布时间:2020-12-16 22:54:02 所属栏目:安全 来源:网络整理
导读:services.xml的配置 ?xml version="1.0" encoding="UTF-8"? ? beans?xmlns= "http://xfire.codehaus.org/config/1.0" ? service ?name服务名( 自己定义的名字 )/name ?namespace名称空间/namespace ?serviceClass接口全路径/serviceClass ?implementationCla

services.xml的配置

<?xml version="1.0" encoding="UTF-8"?>

?

<beans?xmlns="http://xfire.codehaus.org/config/1.0">?

<service>
?<name>服务名(自己定义的名字)</name>
?<namespace>名称空间</namespace>
?<serviceClass>接口全路径</serviceClass>
?<implementationClass>实现类全路径</implementationClass>//注意单词不能拼错,否责出现大问题
?????? </service>

</beans>

访问测试这个服务发布是否成功: ?http://localhost:8080/项目名/services/*(*代表服务名,)

创建客户端
???? (1)导出服务器端的接口jar文件.
???? (2)引入XFire的jar包.?? Core.jar及HTTP Client包
???? (3)创建servlet调用服务.
??????????????????????? Service servicemodel=new ObjectServiceFactory().create(接口名.class);
????????? XFire xfire=XFireFactory.newInstance().getXFire();
????????? XFireProxyFactory factory=new XFireProxyFactory(xfire);
?????????
????????? String url="服务地址";
?????????
????????? 接口名 i=null;
?????????
????????? i=(接口名)factory.create(servicemodel,? url);
?????????
????????? //调用方法


Web .xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
?xmlns="http://java.sun.com/xml/ns/javaee"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
?? <servlet>
??? <display-name>XFireServlet</display-name>
??? <servlet-name>XFireServlet</servlet-name>
??? <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
? </servlet>
? <servlet-mapping>
??? <servlet-name>XFireServlet</servlet-name>
??? <url-pattern>/servlet/XFireServlet/*</url-pattern>
? </servlet-mapping>
? <servlet-mapping>
??? <servlet-name>XFireServlet</servlet-name>
??? <url-pattern>/services/*</url-pattern>
? </servlet-mapping>
? <login-config>
??? <auth-method>BASIC</auth-method>
? </login-config>
? <welcome-file-list>
??? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>
</web-app>

例如:

  1. 第一步:建一个secn的services.xml文件??META-INF?>?xfire?>?services.xml ??
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <beans?xmlns="http://xfire.codehaus.org/config/1.0"> ??
  4. ????<!--?这个是xfirer的配置文件?--> ??
  5. ????<service> ??
  6. ????????<!--?这个是xfire的名字?--> ??
  7. ????????<name>HelloWorldService</name> ??
  8. ????????<!--?这个是名字空间?--> ??
  9. ????????<namespace>urn:helloworld:service:xfire:itcast:cn</namespace> ??
  10. ????????<!--?这个是接口?--> ??
  11. ????????<serviceClass>cn.itcast.xfire.service.HelloWorld</serviceClass> ??
  12. ????????<!--?这个是实现类?--> ??
  13. ????????<implementationClass>cn.itcast.xfire.service.HelloWorldService</implementationClass> ??
  14. ????</service>???? ??
  15. </beans> ??
  16. ??
  17. 第二步:建一个web.xml文件 ??
  18. <!DOCTYPE?web-app ??
  19. ????PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"??
  20. ????"http://java.sun.com/dtd/web-app_2_3.dtd"> ??
  21. ??
  22. <web-app> ??
  23. ????<servlet> ??
  24. ????????<servlet-name>XFireServlet</servlet-name> ??
  25. ????????<display-name>XFire?Servlet</display-name> ??
  26. ????????<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> ??
  27. ????????<!--?通过初始化参数改变xfire配置文件的位置?;如果改改了.那么services.xml就要和web.xml在一起--> ??
  28. ????????<!--? ??
  29. ????????????<init-param> ??
  30. ????????????????<param-name>config</param-name> ??
  31. ????????????????<param-value>services.xml</param-value> ??
  32. ????????????</init-param> ??
  33. ????????--> ??
  34. ????</servlet> ??
  35. ????<servlet-mapping> ??
  36. ????????<servlet-name>XFireServlet</servlet-name> ??
  37. ????????<url-pattern>/servlet/XFireServlet/*</url-pattern> ??
  38. ????</servlet-mapping> ??
  39. ??
  40. ????<servlet-mapping> ??
  41. ????????<servlet-name>XFireServlet</servlet-name> ??
  42. ????????<url-pattern>/services/*</url-pattern> ??
  43. ????</servlet-mapping> ??
  44. </web-app> ??
  45. ??
  46. ??
  47. 测试方法一: ??
  48. @Test??
  49. ????public?void?testXfire1()?throws?Exception{ ??
  50. ????????Service?service?=?new?Service(); ??
  51. ????????Call?call?=?(Call)?service.createCall(); ??
  52. ????????String?url?=?"http://localhost:8080/secn/services/HelloWorldService"?; ??
  53. ????????call.setTargetEndpointAddress(new?URL(url)); ??
  54. ????????call.setOperationName("sayHello"); ??
  55. ????????System.out.println(call.invoke(new?Object[]{"tom"})); ??
  56. ????} ??
  57. ??
  58. 测试方法二:这个url地址一定要加上?wsdl并在?new?Object[]{"tom"})[0]这里要加上[0] ??
  59. @Test??
  60. ????public?void?testXfire2()?throws?Exception{ ??
  61. ????????String?url?=?"http://localhost:8080/secn/services/HelloWorldService?wsdl"?; ??
  62. ????????Client?c?=?new?Client(new?URL(url)); ??
  63. ????????System.out.println(c.invoke("sayHello",?new?Object[]{"tom"})[0]); ??
  64. ????} ??
  65. 测试方法三: ??
  66. @Test??
  67. ????public?void?testXfire3()?throws?Exception{ ??
  68. ????????String?url?=?"http://localhost:8080/secn/services/HelloWorldService"?; ??
  69. ????????ObjectServiceFactory?serviceFactory?=?new?ObjectServiceFactory(); ??
  70. ????????org.codehaus.xfire.service.Service?serviceModel?=?serviceFactory.create(IHelloWorld.class); ??
  71. ????????XFireProxyFactory?proxyFactory?=?new?XFireProxyFactory(); ??
  72. ????????IHelloWorld?hw?=?(IHelloWorld)proxyFactory.create(serviceModel,url); ??
  73. ????????System.out.println(hw.sayHello("Tome")); ??
  74. ????} ??
  75. ??
  76. 第三步: ??
  77. <!--?jsr181的配置,是对于用注释方式生成webService?--> ??
  78. ????<service> ??
  79. ????????<serviceClass>cn.com.secn.xfire.service.jsr181.CustomerService</serviceClass> ??
  80. ????????<!--??serviceFactory>jsr181</serviceFactory?--> ??
  81. ????????<!--?这里用的是#号引用下面的Bean?--> ??
  82. ????????<serviceFactory>#jsr181ServiceFactory</serviceFactory> ??
  83. ??
  84. ????</service> ??
  85. ??
  86. ????<bean?id="config"?class="org.codehaus.xfire.aegis.type.Configuration"> ??
  87. ????????<property?name="defaultExtensibleElements"?value="false"?/> ??
  88. ????????<property?name="defaultExtensibleAttributes"?value="false"?/> ??
  89. ????????<property?name="defaultNillable"?value="false"?/> ??
  90. ????????<property?name="defaultMinOccurs"?value="1"?/> ??
  91. ????</bean> ??
  92. ??
  93. ????<bean?name="jsr181ServiceFactory"?class="org.codehaus.xfire.annotations.AnnotationServiceFactory"> ??
  94. ????????<constructor-arg?ref="xfire.transportManager"?index="0"?/> ??
  95. ????????<constructor-arg?ref="config"?index="1"?type="org.codehaus.xfire.aegis.type.Configuration"?/> ??
  96. ????</bean> ??
  97. ??
  98. CostomerService类: ??
  99. //在这里也可以添服务名 ??
  100. @WebService??
  101. public?class?CustomerService?{ ??
  102. ????private?List<Customer>?customers?=?new?ArrayList<Customer>(); ??
  103. ??
  104. ????public?CustomerService()?{ ??
  105. ????} ??
  106. ??
  107. ????//header?=?true是以头部发送方式??
  108. ????@WebMethod??
  109. ????@WebResult(name?=?"Customers")?//返回类型??
  110. ????public?Collection<Customer>?getCustomers( ??
  111. ????????????@WebParam(name?=?"UserToken",?header?=?true) ??
  112. ????????????UserToken?auth)?{ ??
  113. ????????authorize(auth); ??
  114. ??
  115. ????????return?customers; ??
  116. ????} ??
  117. ??
  118. ????private?void?authorize(UserToken?auth)?{ ??
  119. ????????System.out.println(auth.getUsername()); ??
  120. ????????System.out.println(auth.getPassword()); ??
  121. ????} ??
  122. ??
  123. ????@WebMethod??
  124. ????public?String?addCustomer(@WebParam(name?=?"UserToken",?header?=?true) ??
  125. ????UserToken?auth,?@WebParam(name?=?"customer")?Customer?customer)?{ ??
  126. ????????authorize(auth); ??
  127. ??
  128. ????????customers.add(customer); ??
  129. ??
  130. ????????return?"tommm"; ??
  131. ????} ??
  132. }??

(编辑:李大同)

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

    推荐文章
      热点阅读