配置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> <!-- 请求转发配置 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.ws</url-pattern> </servlet-mapping> <!-- XFire 配置:方便在项目中暴露所有的接口(访问方式:PRO_URL/services/) --> <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>/services/*</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); }
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; } }
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"?> <!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="userServiceImpl" class="com.devil13th.service.UserServiceImpl"></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就正确了