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

Spring集成XFire开发WebService问题

2009-05-08 13:46 507 查看
最近在网上发现有spring集成XFire开发WebService

MyEclipse新建webservice工程在Servlet class选择org.codehaus.xfire.spring.XFireSpringServlet

配置如下:

}

1、配置XFire Servlet

  在web.xml中加入如下配置:

<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>

  2 配置Spring的监听器,同基于spring的Web项目一样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>

  以下是完整的web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <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>
<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>
</web-app>

  3 定义接口及实现服务

  定义接口,这个接口中定义要通过WebService暴露的方法

package org.ccsoft;

publicinterface HelloWS {
 public String sayHello(String sb);

实现服务

package org.ccsoft;

publicclass HelloWSImp implements HelloWS {
 public String sayHello(String sb) {
  // TODO Auto-generated method stub
  return"Hello "+sb;
 }
}

  4 配置服务

  将上文中实现的服务,加入到spring的配置文件中。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="helloWS" class="org.ccsoft.HelloWSImp"/>
<bean name="helloService" class="org.codehaus.xfire.spring.ServiceBean">
<property name="serviceBean" ref="helloWS"/>
<property name="serviceClass" value="org.ccsoft.HelloWS"/>
<property name="inHandlers">
 <list>
  <ref bean="addressingHandler"/>
 </list>
</property>
</bean>

<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
</beans>

实现服务

package org.ccsoft;

publicclass HelloWSImp implements HelloWS {
 public String sayHello(String sb) {
  // TODO Auto-generated method stub
  return"Hello "+sb;
 }
}

  4 配置服务

  将上文中实现的服务,加入到spring的配置文件中。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="helloWS" class="org.ccsoft.HelloWSImp"/>
<bean name="helloService" class="org.codehaus.xfire.spring.ServiceBean">
<property name="serviceBean" ref="helloWS"/>
<property name="serviceClass" value="org.ccsoft.HelloWS"/>
<property name="inHandlers">
 <list>
  <ref bean="addressingHandler"/>
 </list>
</property>
</bean>

<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
</beans>




代码考过来启动服务发现出现如下问题

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Line 10 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".

提示找不到beans标签。

找了以下资料发现是

<beans>标签问题,把<beans标签的schema去掉,加上<DOCTYPE>

修改spring配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloWS" class="org.ccsoft.HelloWSImp" />
<bean name="helloService"
class="org.codehaus.xfire.spring.ServiceBean">
<property name="serviceBean" ref="helloWS" />
<property name="serviceClass" value="org.ccsoft.HelloWS" />
<property name="inHandlers">
<list>
<ref bean="addressingHandler" />
</list>
</property>
</bean>
<bean id="addressingHandler"
class="org.codehaus.xfire.addressing.AddressingInHandler" />
</beans>

再次启动服务ok;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: