webservice-之使用xfire+spring开发(转自勇哥的BLOG)
1
、想要发布成webservice的类,必须实现接口(Spring2.0,Xfile1.2)
2
、3个配置文件(后面详细说)
----------------------------------------------
HelloRemote.java
package test;
public interface HelloRemote {
public String hello();
public String helloTo(String name);
public String getJsonData(String pageIndex,String pageSize);
}
----------------------------------------------
HelloBean.java
package test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.codehaus.xfire.transport.http.XFireServletController;
public class HelloBean implements HelloRemote
{
public String hello()
{
return "hello";
}
public String helloTo(String name)
{
return " hello " + name + "!";
}
private String example()
{
//
使用session
HttpServletRequest request = XFireServletController.getRequest();
HttpSession session = request.getSession();
return "
请求地址:"+request.getLocalAddr()
+",
会话创建时间:"+session.getCreationTime();
}
public String getJsonData(String pageIndex, String pageSize)
{
String rtnValue = "";
rtnValue = example();
return rtnValue;
}
}
----------------------------------------------
web.xml
<?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>
/WEB-INF/classes/applicationContext*.xml
classpath:org/codehaus/xfire/spring/xfire.xml
</param-value>
</context-param>
<!--
启动时加载SpringContextServlet -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!-- XFire
配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这里注意:classpath:org/codehaus/xfire/spring/xfire.xml必须要写进去。
----------------------------------------------
xfire-servlet.xml
新建这个文件,并且和web.xml放在同一个文件夹。
注意: 名称和位置都不能变。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/myHelloService.ws"><!--
暴露的服务名称 -->
<ref bean="myTest" />
</entry>
</map>
</property>
</bean>
<bean id="myTest" parent="webService"
class="org.codehaus.xfire.spring.remoting.XFireExporter">
<!--
定义实现类(指向applicationContext-webService.xml) -->
<property name="serviceBean">
<ref bean="myHelloBean" />
</property>
<!--
定义接口类 -->
<property name="serviceClass">
<value>test.HelloRemote</value>
</property>
</bean>
<!-- webService
基本配置 -->
<bean id="webService"
class="org.codehaus.xfire.spring.remoting.XFireExporter"
abstract="true">
<property name="serviceFactory">
<ref bean="xfire.serviceFactory" />
</property>
<property name="xfire">
<ref bean="xfire" />
</property>
</bean>
</beans>
----------------------------------------------
spring
的配置文件 applicationContext-webService.xml
<?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="myHelloBean" class="test.HelloBean"></bean>
</beans>