1、給web項(xiàng)目添加web service支持,web.xml文件會(huì)自動(dòng)修改。
<SERVLET-NAME>XFireServlet</SERVLET-NAME><SERVLET-CLASS>org.codehaus.xfire.transport.http.XFireConfigurableServlet</SERVLET-CLASS><LOAD-ON-STARTUP>0</LOAD-ON-STARTUP><SERVLET-MAPPING><SERVLET-NAME>XFireServlet</SERVLET-NAME><URL-PATTERN>/services/*</URL-PATTERN></SERVLET-MAPPING>2、 創(chuàng)建對(duì)外提供的服務(wù)接口和實(shí)現(xiàn)類
接口:HelloService
package com.xaccp.service;public interface HelloService { public String sayHello(String name);}實(shí)現(xiàn)類:HelloServiceImpl
package com.xaccp.service;public class HelloServiceImpl implements HelloService { public String sayHello(String name) { System.out.println("來(lái)自客戶端的用戶:" + name); return "服務(wù)器說(shuō)你好:" + name; }}3、 發(fā)布服務(wù)
上面發(fā)布后 services.xml 就發(fā)生下面的變化
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>Hello</name> <serviceClass>com.xaccp.service.HelloService</serviceClass> <implementationClass> com.xaccp.service.HelloServiceImpl </implementationClass> <style>wrapped</style> <use>literal</use> <scope>application</scope> </service></beans>到此服務(wù)器端就ok了,測(cè)試一下是否ok,發(fā)布服務(wù)到Tomcat;啟動(dòng)Tomcat,訪問(wèn):http://localhost:8080/helloService/services,出現(xiàn)如下信息說(shuō)明發(fā)布成功
4、 客戶端
創(chuàng)建web項(xiàng)目(需要添加xFire包)或webService項(xiàng)目,需要服務(wù)器提供接口:
測(cè)試類:
public static void main(String[] args) {// 創(chuàng)建服務(wù)的元數(shù)據(jù)Service serviceModel = new ObjectServiceFactory().create(HelloService.class);// 創(chuàng)建服務(wù)的代理XFire xfire = XFireFactory.newInstance().getXFire();XFireProxyFactory factory = new XFireProxyFactory(xfire);// 指定服務(wù)的地址 應(yīng)用程序地址+“/services/”+服務(wù)名稱String serviceUrl = "http://192.168.0.98:8080/helloService/services/Hello";HelloService client = null;try {// 創(chuàng)建服務(wù)的對(duì)象client = (HelloService) factory.create(serviceModel, serviceUrl);// 調(diào)用服務(wù)String res = client.sayHello("哈哈");System.out.println(res);} catch (MalformedURLException e) {System.out.println(" " + e.getMessage());}}本文由北大青鳥論壇投稿,原文地址:http://www.beiniao.net/thread-3174-1-1.html
新聞熱點(diǎn)
疑難解答
圖片精選