`
devil13th
  • 浏览: 42095 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring集成xfire教程

阅读更多

作者:ThirdteenDevil 十三妖

qq:181907667

 

 

配置web.xml

<!-- SPRING 配置 -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		classpath*:applicationContext.xml,
		classpath:org/codehaus/xfire/spring/xfire.xml
	</param-value>
</context-param>
<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>

 注意要引用这句:classpath:org/codehaus/xfire/spring/xfire.xml这个文件在xfire-all-1.2.6.jar中

 

想要发布的接口

package com.devil13th.service;
import java.util.List;
public interface IUserService {
	public User findUser(String usrName);
	public List findAllUser();
	public String hello(String usrName);
	public User createUser(Param param);
}

 

需要的bean

User.java:

package com.devil13th.service;

public class User {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

Param.java

package com.devil13th.service;

public class Param {
	private String mes;

	public String getMes() {
		return mes;
	}

	public void setMes(String mes) {
		this.mes = mes;
	}
}

 

 

想要发布的接口实现

package com.devil13th.service;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements IUserService{

	public List findAllUser() {
		List l = new ArrayList();
		for(int i = 0 ; i < 5 ; i++){
			User u = new User();
			u.setAge(i);
			u.setName("name_" + i);
			l.add(u);
		}
		return l;
	}

	public User findUser(String usrName) {
		User u = new User();
		u.setAge(5);
		u.setName(usrName);
		return u;
	}

	public String hello(String usrName) {
		return "hello " + usrName;
	}
	
	public User createUser(Param param){
		User u = new User();
		u.setName(param.getMes());
		u.setAge(20);
		return u;
	};

}

 

服务接口相关的xml

注意:方法中有list或者collection中的返回类型,如果用的是jdk1.4或以下 则需要配置下面的文件,如果是jkd1.4以上则必须使用泛型

<?xml version="1.0" encoding="UTF-8"?>
<mappings>
    <mapping>
		<-- 配置方法的方法名 -->
       <method name="findAllUser">
	      <-- List泛型的类型 -->
          <return-type componentType="com.devil13th.service.User" />
       </method>
       <method name="createUser">
	   	<-- 参数类型 index是参数的位置 从0开始 -->
       	<parameter index="0" componentType="java.lang.String" />
       </method>
    </mapping>    
</mappings>

 注意:这个文件必须要放在和你的接口相同的目录下!

 

spring配置发布的接口实现

xfire-servlet.xml 放在webroot下的WEB-INF文件夹下

		
<?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.5.xsd">

	<bean id="userServiceImpl" class="com.devil13th.service.UserServiceImpl"></bean>
</beans>

 

配置xfire-servlet.xml

xfire-servlet.xml 放在webroot下的WEB-INF文件夹下

	
<?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="/test.ws">
					<!-- 关联的bean , 对应applicationContext.xml中id是test的bean -->
					<ref bean="test" />
				</entry>
			</map>
		</property>
	</bean>

	<bean id="test" parent="webService"
		class="org.codehaus.xfire.spring.remoting.XFireExporter">
		<!-- 定义发布的接口实现 -->
		<property name="serviceBean">
			<ref bean="testServiceImpl" />
		</property>
		
		<!-- 定义发布的接口 -->
		<property name="serviceClass">
			<value>com.thd.service.TestService</value>
		</property>
	</bean>

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

 其中有注释的地方需要根据自己的项目去修改,其他照搬

 

测试

打开web服务,ie地址栏输入 http://127.0.0.1:8080/m/test.ws?wsdl 能打开一个xml就正确了

 

 

 

以下是客户端调用

 

客户端引入jar

xfire相关jar引入到项目中

 

客户端建立接口

package com.devil13th.webservice;

import java.util.List;

import com.devil13th.service.Param;
import com.devil13th.service.User;

public interface IUserService {
	public User findUser(String usrName);
	public List findAllUser();
	public String hello(String usrName);
	public User createUser(Param param);
}

 

接口所需要的bean

这里的bean和服务器上的bean相同。代码不再重复拷贝服务器端上的bean
注意:客户端用到的bean不仅内容要和服务器上的相同并且包名都必须是一样的!

 

客户端接口所用到xml

注意:方法中有list或者collection中的返回类型,如果用的是jdk1.4或以下 则需要配置下面的文件,如果是jkd1.4以上则必须使用泛型

<?xml version="1.0" encoding="UTF-8"?>
<mappings>
    <mapping>
		<-- 配置方法的方法名 -->
       <method name="findAllUser">
	      <-- List泛型的类型 -->
          <return-type componentType="com.devil13th.service.User" />
       </method>
       <method name="createUser">
	   	<-- 参数类型 index是参数的位置 从0开始 -->
       	<parameter index="0" componentType="java.lang.String" />
       </method>
    </mapping>    
</mappings>
	

 注意:这个文件必须要放在和你的接口相同的目录下!

 

客户端调用webservice测试

	
package com.devil13th.webservice;

import java.util.List;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.devil13th.service.Param;
import com.devil13th.service.User;

public class Client {
	public static void main(String args[]){
		 Service serviceModel = new ObjectServiceFactory().create(IUserService.class);   
		 XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());   
		 String helloWorldURL = "http://127.0.0.1:8080/sx/service.ws";  
		 try{
			 IUserService ts = (IUserService) factory.create(serviceModel,helloWorldURL);
			 	
			 	//createUser方法调用
			 	System.out.println("====== createUser方法调用 =======");
			 	Param p = new Param();
			 	p.setMes("ThirdteenDevil");
			 	User u = ts.createUser(p);
			 	System.out.println(u.getName());
			 	System.out.println();
			 	
			 	//findUser 方法调用
			 	System.out.println("====== findUser 方法调用 =======");
			 	User u2 = ts.findUser("devil13th");
			 	System.out.println(u2.getName());
			 	System.out.println();
			 
				//findAllUserAllUsr 方法调用
			 	System.out.println("====== findAllUserAllUsr 方法调用 =======");
				List l = ts.findAllUser();
				System.out.println(l.size());
				for(int i = 0 , j = l.size(); i < j ; i++){
					User ux = (User)l.get(i);
					System.out.println(ux.getName());
				}

		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 
	}
}

 

常遇到的异常解决办法

1.java.io.FileNotFoundException: class path resource [org/codehaus/xfire/spring/xfire.xml] cannot be opened because it does not exist
没有找到org/codehaus/xfire/spring/xfire.xml。查看你的xfire所用的jar中是否包括了org/codehaus/xfire/spring/xfire.xml文件
2.java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xfire-servlet.xml]
查看项目的webroot下面是否有/WEB-INF/xfire-servlet.xml文件
3.org.codehaus.xfire.XFireRuntimeException: Cannot create mapping for java.util.List, unspecified component type for return type of method findAllUser in interface com.devil13th.service.IUserService
发布的接口一定是有List或collection等返回类型,而且没有配置这些返回类型的.xml文件或是没有配置泛型的类型。查看【服务接口相关的xml】部分中的教程内容

 

 

ps:全部代码在附件中

 

 

分享到:
评论
1 楼 wcg_108 2012-07-05  
不行,没有包;自己添好像又运行不了

相关推荐

Global site tag (gtag.js) - Google Analytics