Myeclipse spring xfire 开发 webservice
XFire是一个简化WebService开发的开源项目,通过Spring和XFire的结合可以大大简化基于Spring Framework的应用中的WebService开发。
一:开发环境
Myeclipse :
MyEclipse Enterprise Workbench Version: 7.0 Milestone-1
spring :spring 2.5
xfire :1.2
tomcat:6.0
二: 新建 web service 工程
从菜单按照下面的路径依次进入:
file 》new project 》myeclipse 》web project
之后,到了下面的页面。在这页面填入web 项目的名称:Hello
2 加入 spring 的包
从 myeclipse》project capabilities 》 add spring capabilities 进入下面的页面
3 加入 xfire 的包
从 myeclipse》project capabilities 》 add xfire capabilities 进入下面的页面
在 servlet class 选项选择 org.codehaus.xfire.spring.XfireSpringServlet 类,而不是选择默认的org.codehaus.xfire.transport.http.XfireConfigurableServlet。
之后,自动在web配置文件中加入了xfire的servlet配置,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
二 在 web配置文件中加入spring监听器,如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:org/codehaus/xfire/spring/xfire.xml,classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
三 编写代码
(1) 接口类
package com.imti;
public interface Hello {
public String sayHello(String sb);
}
(2) 实现类
package com.imti;
public class HelloWS implements Hello{
public String sayHello(String somebody) {
return"你好: "+somebody;
}
}
四 配置服务
将上文中实现的服务,加入到spring的配置文件中,代码如下:
<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
<bean id="helloWS" class="com.imti.HelloWS"/>
<bean name="helloService" class="org.codehaus.xfire.spring.ServiceBean">
<property name="serviceBean" ref="helloWS"/>
<property name="serviceClass" value="com.imti.Hello"/>
<property name="inHandlers">
<list>
<ref bean="addressingHandler"/>
</list>
</property>
</bean>
五 部署服务
用myeclipse 部署服务,并启动tomcat。
从浏览器访问到以下页面:
1 http://localhost:8080/Hello/services/helloService?wsdl
2 http://localhost:8080/Hello/services/Hello?wsdl
六 可能出现的问题
1 spring 配置文件异常:Document root element "beans", must match DOCTYPE root "null"
分析:这表明审判日那个需要XML DTD的设置方式,而项目中很显然这是XML Schema的设置方式。我把spring 配置文件改为XML DTD的设置方式,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--<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.5.xsd">
-->
<beans>
<bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler"/>
<bean id="helloWS" class="com.imti.HelloWS"/>
<bean name="helloService" class="org.codehaus.xfire.spring.ServiceBean">
<property name="serviceBean" ref="helloWS"/>
<property name="serviceClass" value="com.imti.Hello"/>
<property name="inHandlers">
<list>
<ref bean="addressingHandler"/>
</list>
</property>
</bean>
</beans>
七 webservices 客户端开发
就在本项目开发客户端。
从 new》other 》 myeclipse 》 web services client 进入下面的页面
选择 xfire 单选项,进入下一步页面。
在这个页面的WSDL URL 处填入:
http://localhost:8080/Hello/services/Hello?wsdl
新建客户类所在的包:com.imti.client。点击下一步完成。
从IDE中可以看出,总共生成6个类。
最后,加入代码:
在helloClient.java文件的的main函数中加入:
System.out.println(service.sayHello("tech fan"));
helloClient.java文件的的main函数完整的代码如下:
public static void main(String[] args) {
HelloClient client = new HelloClient();
//create a default service endpoint
HelloPortType service = client.getHelloHttpPort();
System.out.println(service.sayHello("tech fan"));
System.out.println("test client completed");
System.exit(0);
}
将项目作为Application编译执行,
你好: tech fan
test client completed