您的位置:首页 > 编程语言 > Java开发

xfire+spring2整合开发webservice

2015-03-18 10:51 260 查看
 开发工具是myeclipse7.1,服务器是tomcat6.0,,用的xfire1.2.6和Spring 2.0,虽然MyEclipse自带了开发xfire的插件,但为了不依赖开发工具和引用更新的包,所以建议手动创建。
     首先需要了解一下spring 、xfire 和webservice的概念:

Spring是目前最流行的JavaEE
Framework,但是使用Spring的Spring-WS开发WebService却十分繁琐。XFire是一个简化WebService开发的开源项目,通过Spring和XFire的结合可以大大简化基于Spring
Framework的应用中的WebService开发。

XFire是完全基于流数据处理进行工作的系统,这意味着XFire不是将整个SOAP文档缓存在内存中,而是以管道的方式接收SOAP流数据。这种工作方式的转变带来了可观的性能回报,同时节省了内存的占用。

对于习惯了Axis、GLUE等这些基于DOM处理模型Web
Service框架的开发者来说,需要一些时间来适应这种转变。

XFire从管道中接收一个SOAP请求到返回一个SOAP响应,会经历一系列的阶段。在管道调用的任何一个阶段,XFire都可以添加一些额外的 Handler,在对消息进行加工处理后再传入到下一个阶段中。图1展示了XFire管道从接收SOAP请求到返回SOAP响应所经历的所有阶段:

图1 XFire Web Service请求和响应的过程


在SOAP请求消息对Web
Service发起真正调用之前,分别会经过传输(Transport)、预转发(PreDispatch)、转发(Dispatch)、策略实施(Policy)、用户信息处理(User)、预调用(PreInvoke)、服务调用(Service
Invocation)等阶段。当,Web Service调用后,XFire生成响应SOAP消息并通过管道发送给客户端请求者,这一过程会先后经历调用后(PostInvoke)、用户信息处理(User)、策略实施(Policy)、传输(Transport)这四个阶段。每一个阶段都是一个可控点,通过编写并注册一些相应的Handler就可以实施一些额外处理逻辑,如审计、SOAP消息加密、签名、压缩等。

将POJO Bean导出为Web
Service


通过XFire为Spring提供的服务导出器可以轻松地将POJO导出为标准的Web
Service,此外,XFire还允许我们使用JSR
181注解对POJO进行标注,无需使用XML配置就可以导出为Web
Service,各种复杂的转换细节被巧妙地隐藏在XFire之中。

XFire可以很好的集成到Spring中,Spring的代码已经做了这方面的集成。

1、将xfire与spring的JAR包放到web 项目的classPath,(注意:把xfire自带的Spring1.2.6删掉,它会和spring2.0中的spring.jar产生冲突)

2、修改web.xml,使其支持Spring 与xfire,内容如下:
<?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">

 

 <context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>classpath:org/codehaus/xfire/spring/xfire.xml,

  /WEB-INF/applicationContext-beans.xml

  </param-value>

 </context-param>

 

 <servlet>

  <servlet-name>xfire</servlet-name>

  <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>

 </servlet>

 

 <servlet-mapping>

  <servlet-name>xfire</servlet-name>

  <url-pattern>/services/*</url-pattern>

 </servlet-mapping>

 

 <listener>

  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

 </listener>

 

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

3、配置(spring)applicationContext.xml

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

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xmlns:aop="http://www.springframework.org/schema/aop"

      xmlns:tx="http://www.springframework.org/schema/tx"

      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

 

 <bean id="mathService" class="com.zjh.manager.MathServiceImpl" />

 

 <bean id="mathExporters" class="org.codehaus.xfire.spring.remoting.XFireExporter">

  <property name="serviceFactory" ref="xfire.serviceFactory" />

  <property name="xfire" ref="xfire" />

  <property name="serviceBean" ref="mathService" />

  <property name="serviceClass" value="com.zjh.manager.MathService" />

  <property name="name" value="mathWebService" />

 </bean>

 

</beans>

4、创建一个接口和一个实现类

package com.zjh.manager;

public interface MathService {

 public String add(int a,int b);

}

package com.zjh.manager;

public class MathServiceImpl implements MathService {

 public String add(int a, int b) {

      long result = a+b;

  return "计算的结果:"  + result;

 }

}

这样便生成一个web Service,可以通过域名 http://localhost:8080/testwebservice_server/services/mathWebService?wsdl    
来访问,并编写客户端代码了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: