作者: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:全部代码在附件中
分享到:
相关推荐
spring xfire spring 集成xfire 比较好的一种方式
spring+xfire集成解决方案spring+xfire集成解决方案
xFire与Spring集成xFire与Spring集成xFire与Spring集成xFire与Spring集成xFire与Spring集成
spring集成xfire webservice实现远程调用 将项目发布后,点击http://localhost:8080/SpringWebServiceTest/services/HelloWS?wsdl即可 其中也有客户端的调用,自己试试吧。
spring+xfire( 编写webservice完整配置+案例)
相对于Axis来说,目前XFire相对受欢迎,加上其提供了和Spring集成的支持,在目前的Web Service开源社区拥有众多的追随者。并且因为XFire为Spring提供的支持,使得我们可以很容易在Spring中使用XFire构建Web Service...
这里面介绍了关于使用spring 集成了xfire的功能, 同时提供了部分源码, 关于web.xml中的配置 关于spring.xml的配置 关于spring.xfire.xml配置 关于项目框架的搭建 如有需要全部源码的读者可以联系我: QQ :...
解决了Spring2.0和XFire1.2.6整合出错的问题 如果你觉得我骗分请不要下,谢谢!
spring-mvc与xfire的集成 对于这样的集成,看看里面的配置文件就行了 一个简单的接口,对应一个简单的实现。 然后在配置文件里指明就可以了。 DispatcherServlet本身会管理xfire的请求 配置文件中配置的key可以理解...
xfireSpring集成需要的Jar包
spring和xfire集成的时候修要的jar文件,spring需要的jar文件自己另行下载。.
xfire集成了spring的jar包,放到工程中引用即可用xfire集成spring的方式进行webservice接口开发
实现 xFire与 Spring 的集成
webservice xfire jar包 spring开发,集成spring开发 webservice xfire jar包 spring开发,集成spring开发 有问题随时联系博客
使用XFire+Spring构建Web Service。自己参考网上的讲解写的例子。里面有help.txt 介绍了我参考的xfire+spring集成的网址。
NULL 博文链接:https://jilongliang.iteye.com/blog/1441583
XFire和spring集成设置,自己写的一个小DEMO,
XFire与Spring集成WebService客户端的两种开发方式.pdfXFire与Spring集成WebService客户端的两种开发方式.pdfXFire与Spring集成WebService客户端的两种开发方式.pdfXFire与Spring集成WebService客户端的两种开发方式...