XFire实现WebService一:使用XfireConfigurableServlet
使用XFire进行web services开发有三种方式:
1. 不集成Spring: 配置services.xml 文件和web.xml(配置org.codehaus.xfire.transport.http.XFireConfigurableServlet servlet)
2. Spring使用XFireSpringServlet方式。
3.Spring集成使用org.springframework.web.servlet.DispatcherServlet方式
使用XfireConfigurableServlet进行实现的步骤:
1、定义服务接口;
2、实现服务接口;
3、更新web.xml添加XFire相关的servlet(即添加XfireConfigurableServlet)
4、创建services.xml文件
5、测试web services是否部署成功
1、定义服务接口 |
package com.mybank.xfire.example; public interface IBankingService { public String transferFunds( String fromAccount, String toAccount, double amount, String currency); } |
注:服务接口不能少,客户端访问web services就是通过接口进行访问的,实现类对客户端是透明的。 |
2、实现服务接口 |
package com.mybank.xfire.example; import java.text.NumberFormat; i import java.text.DecimalFormat; public class BankingService implements IBankingService { public BankingService(){ } public String transferFunds( String fromAccount, String toAccount, double amount, String currency){ String statusMessage = ""; try { NumberFormat formatter = new DecimalFormat("###,###,###,###.00"); statusMessage = "COMPLETED: " + currency + " " + formatter.format(amount)+ " was successfully transferred from A/C# " + fromAccount + " to A/C# " + toAccount; } catch (Exception e){ statusMessage = "BankingService.transferFunds(): EXCEPTION: " + e.toString(); } return statusMessage; } } |
注:BankingService的transferFunds返回类型是String,如果是复杂类型需要Aegis进行数据绑定。 |
3、web.xml添加XfireConfigurableServlet |
<servlet> <servlet-name>XFireServlet</servlet-name> <display-name>XFire Servlet</display-name> <servlet-class>org.codehaus.xfire.transport.http.XfireConfigurableServlet </servlet-class> <!--配置services.xml路径以便加载services.xml管理web服务,不配置则到默认路径加载--> <init-param> <param-name>config</param-name> <param-value>services.xml</param-value> </init-param> </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> |
4、创建services.xml文件 |
<beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>Banking</name> <namespace>mybank</namespace> <serviceClass>com.mybank.xfire.example.IBankingService</serviceClass> <implementationClass>com.mybank.xfire.example.BankingService</implementationClass> </service> </beans> |
注:services.xml定义了web服务,如此services.xml就定义了Banking服务,其服务接口和实现类分别是IBankingService和BankingService。XfireConfigurableServlet就是通过加载services.xml来管理web服务的。加载的默认路径为WEB-INF\classes\META-INF\xfire文件夹。可以在web.xml配置XfireConfigurableServlet时至定义路径 <init-param> <param-name>config</param-name> <param-value>services.xml</param-value> </init-param> |
5、测试web服务是否部署成功有两种方法:一是通过浏览器进行测试,二是通过客户端进行测试。 |
/×客户端测试方法×/ public String callWebService( String fromAccount, String toAccount, double amount, String currency) throws MalformedURLException, Exception {
Service serviceModel = new ObjectServiceFactory().create(IBankingService.class); System.out.println("callSoapServiceLocal(): got service model." ); XFire xfire = XFireFactory.newInstance().getXFire(); XFireProxyFactory factory = new XFireProxyFactory(xfire); String serviceUrl = "http://localhost:8080/websvc/services/Banking"; IBankingService client = null; try { client = (IBankingService) factory.create(serviceModel, serviceUrl); } catch (MalformedURLException e) { System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString()); } String serviceResponse = ""; try { serviceResponse = client.transferFunds(fromAccount, toAccount, amount, currency); } catch (Exception e){ System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString()); serviceResponse = e.toString(); } System.out.println("WsClient.callWebService(): status=" + serviceResponse); return serviceResponse; } |
浏览器测试:http://localhost:8080/websvc/services/Banking?wsdl 查看web服务发布情况 |