Spring集成Xfire
用XFire 打造webServices
一:下载XFire1.26
官网地址:http://xfire.codehaus.org/Download
下载 xfire-distribution-1.2.6.zip 与 xfire-all-1.2.6.jar 包,如单独下载jar包,会发生譬如找不到Spring依赖或Ant依赖等少包问题,因此最好把依赖jar包与核心jar包都下了,甩到lib中,万事无忧。
二:Xfire 服务端 VS Xfire客户端
(一):Xfire 服务端构建
1:新建web工程 TestXfire ,将刚下载到的包括核心与依赖jar都丢到web-inf/lib中作为外部jar包引用。
2:打开xml,加入以下代码,将Xfire解析加到web工程中,在init-param的value属性中,规定了Xfire在哪里查找xfire配置文件,可根据自己需要更改文件位置。
- ..........
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>xfire/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>
- ..........
3:新建包:xfire,在之下新建 services.xml ,作为 xfire 向外部暴露的web服务配置文件。
4:
新建包:com.testxfire.dao ,之下新建web服务接口:ICalculation.java ,接口中声明了用于向外部暴露的计算服务方法:
- package com.testxfire.dao;
- public interface ICalculation {
- public String CalculationMain(int type,double p1,double p2);
- }
新建包:com.testxfire.dao.impl ,之下ICalculation实现类:CalculationImpl.java
- package com.testxfire.dao.impl;
- import java.text.DecimalFormat;
- import com.testxfire.dao.ICalculation;
- public class CalculationImpl implements ICalculation {
- public String CalculationMain(int type, double p1, double p2) {
- String str = "";
- try{
- DecimalFormat df=new DecimalFormat("#.00");
- switch (type) {
- case 1:
- str = df.format(p1 + p2);
- break;
- case 2:
- str = df.format(p1 - p2);
- break;
- case 3:
- str = df.format(p1 * p2);
- break;
- case 4:
- str = df.format(p1 / p2);
- break;
- default:
- str = "无效的运算符";
- break;
- }
- }catch(Exception es){
- str = "运算发生未知错误";
- }
- return str;
- }
- }
5:因为第4步新建了用于web服务的接口及实现类,因此需要在xfire的配置文件 services.xml 中声明它们,这样Xfire可通过读取配置文件得到需要向外暴露的接口:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <name>calculation</name>
- <namespace>ws_cal</namespace>
- <serviceClass>com.testxfire.dao.ICalculation</serviceClass>
- <implementationClass>com.testxfire.dao.impl.CalculationImpl</implementationClass>
- </service>
- </beans>
6:整个过程完成后,部署到Tomcat运行,输入 http://localhost:7999/TestXfire/services/calculation?wsdl ,如可以看到浏览器找到了解释此 webServices 系统的wsdl文件,说明服务端构建成功。
(二):Xfire 客户端构建
1:新建web工程TestXfireClient,同样把下载的xfire包丢到lib中引用。
2:新建包:com.clientbean ,之下新建 Servlet类:WebClientBean.java
- package com.clientbean;
- import java.io.IOException;
- import java.net.URL;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.codehaus.xfire.client.Client;
- public class WebClientBean extends HttpServlet {
- protected void doGet(HttpServletRequest req, HttpServletResponse resp){
- try {
- doPost(req, resp);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- String p1 = req.getParameter("p1");
- double p = Double.parseDouble(p1);
- Client client;
- try {
- client = new Client(new URL("http://localhost:7999/TestXfire/services/calculation?wsdl"));
- Object[] results = client.invoke("CalculationMain", new Object[] {new Integer(1), p,p});
- System.out.println((String) results[0]);
- System.out.println(p1);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
3:将WebClientBean.java 在web.xml 中声明为servlet。
- <servlet>
- <servlet-name>wsclient</servlet-name>
- <servlet-class>com.clientbean.WebClientBean</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>wsclient</servlet-name>
- <url-pattern>/wsclient</url-pattern>
- </servlet-mapping>
4:在首页 index.jsp 中建立一个表单提交,提交到WebClientBean
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>调用远程webservice</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- </head>
- <body>
- <form action="/TestXfireClient/wsclient">
- <input type="hidden" id="p1" name="p1" value="4.5" />
- <input type="submit" value="按下调用webServices计算 4.5 + 4.5" />
- </form>
- </body>
- </html>
可以看到在WebClientBean.java中,XFire调用远程web服务需要在获得WSDL描述文件后,必须设定一个远程代理(代理需传入调用的方法名、方法参数)。XFire 只用了2句代码就调用了一个远程web服务并返回结果,当然这份代码前提是web服务没有进行加密。因此XFire相比于其他webservice如ASIX,有着很大的便捷性,更重要XFire是新一代的WebService,是基于SOAP的。部署到Tomcat运行后,按下index.jsp的按钮,转到eclipse的控制台,可以看到 WebClientBean.java 中 System.out.println((String) results[0]); 这句代码打印出调用后的结果:9.0 。
Spring 集成 Xfire
1:新建web工程TestSpringXfire,加入下载的Xfire包,然后从中删除掉Spring1.26 jar包,以免与后面的Spring2.0发生冲突,另外加入Spring2.0 core、web包。
2:在web.xml中加入Spring、Xfire解析。
Spring:
- ..........
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:org/codehaus/xfire/spring/xfire.xml,/WEB-INF/ApplicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- ..........
Xfire:
- ...........
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</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>
- ...........
3:将之前TestXfire新建的接口与其实现类粘贴到此工程中来
4:在web.xml中声明Spring配置文件的位置加入Spring配置文件,此工程配置文件名为:ApplicationContext.xml 。打开此xml,在<beans>节点中加入:
- ..........
- <bean id="calWS" class=" com.testxfire.dao.impl.CalculationImpl"/>
- <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler" />
- <bean name="wsService" class="org.codehaus.xfire.spring.ServiceBean" >
- <property name="serviceBean" ref="calWS" ></property>
- <property name="serviceClass" value="com.testxfire.dao.ICalculation"></property>
- <property name="inHandlers">
- <list>
- <ref bean="addressingHandler" />
- </list>
- </property>
- </bean>
- ..........
此段代码主要是将web服务暴露的外部接口交给了Spring去管理,并且由spring去决定是否开放此web服务。
5:部署到Tomcat运行,输入:http://localhost:7999/TestSpringXfire/services,可以web服务页面,上面标注了此web服务开放的服务接口。
6:第5步中标注的服务接口后有每个服务接口的wsdl链接,点击后将wsdl的url复制,替换掉TestXfireClient工程中WebClientBean.java中用到获取wsdl描述文件的wsdl url。运行TestXfireClient,可以看到后台打印出调用结果。