原文地址:http://www.javaeye.com/topic/70005
1、拷贝xfire相关的jar到classpath;
2、在web.xml配置xfire的servlet及其url映射
xml 代码
- <servlet>
- <servlet-name>xfireservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationparam-name>
- <param-value>classpath:xfire-servelet.xmlparam-value>
- init-param>
- servlet>
xml 代码
- xml version="1.0" encoding="UTF-8"?>
- >
- <beans>
- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="urlMap">
- <map>
- <entry key="/SAService">
- <ref bean="SAServiceWS" />
- entry>
- <entry key="/BulletinService">
- <ref bean="BulletinServiceWS" />
- entry>
- map>
- property>
- bean>
- <bean id="SAServiceWS" class="org.codehaus.xfire.spring.remoting.XFireExporter">
- <property name="serviceFactory">
- <ref bean="xfire.serviceFactory" />
- property>
- <property name="xfire">
- <ref bean="xfire" />
- property>
- <property name="serviceBean">
- <ref bean="SAService" />
- property>
- <property name="serviceClass">
- <value>net.gbicc.sa.service.SAServicevalue>
- property>
- bean>
- <bean id="BulletinServiceWS" class="org.codehaus.xfire.spring.remoting.XFireExporter">
- <property name="serviceFactory">
- <ref bean="xfire.serviceFactory" />
- property>
- <property name="xfire">
- <ref bean="xfire" />
- property>
- <property name="serviceBean">
- <ref bean="bulletinService" />
- property>
- <property name="serviceClass">
- <value>net.gbicc.swm.service.BulletinServicevalue>
- property>
- bean>
- beans>
5、可是,一般情况下不会这么顺利的(参考http://wiki.springside.org.cn/display/springside/ XFire的2.3),我们还需要为那些返回值为集合类型的方法以及包含集合属性的model做一些特殊配置:
在SAService.java同一目录新建SAService.aegis.xml
xml 代码
- xml version="1.0" encoding="utf-8"?>
- <mappings>
- <mapping>
- <method name="getPrivileges">
- <return-type componentType="net.gbicc.sa.domain.Privilege" />
- method>
- <method name="getDepts">
- <return-type componentType="net.gbicc.sa.domain.Department" />
- method>
- <method name="getRoles">
- <return-type componentType="net.gbicc.sa.domain.Role" />
- method>
- <method name="getUsers">
- <return-type componentType="net.gbicc.sa.domain.User" />
- method>
- <method name="getUsersByDept">
- <return-type componentType="net.gbicc.sa.domain.User" />
- method>
- <method name="getUsersByRole">
- <return-type componentType="net.gbicc.sa.domain.User" />
- method>
- mapping>
- mappings>
xml 代码
- xml version="1.0" encoding="utf-8"?>
- <mappings>
- <mapping>
- <property name="roles" ignore="true" />
- mapping>
- mappings>
7、可是,能够生成WSDL并不意味着程序已经可以访问这个Webservice,还要用单元测试来验证一下:
java 代码
- public class WSTest extends AbstractXFireSpringTest {
- private Log log = LogFactory.getLog(WSTest.class);
- protected ApplicationContext createContext() {
- return new ClassPathXmlApplicationContext(new String[] { "classpath*:applicationContext*.xml", "classpath*:xfire-servelet.xml" });
- }
- public void testSAWS() throws Exception {
- Service serviceModel = new ObjectServiceFactory().create(SAService.class);
- XFireProxyFactory factory = new XFireProxyFactory(getXFire());
- SAService service = (SAService) factory.create(serviceModel, "xfire.local://SAService");
- List list = service.getPrivileges();
- assertNotNull(list);
- }
- }
因为继承了 xfire提供的测试基类 AbstractXFireSpringTest,使得我们可以不起服务就能测这个webservice。
8、如果运气不太差,应该可以看见绿条庆祝胜利了,可是,我却遇到了一个奇怪错误:org.codehaus. xfire.fault.XFireFault: No write method for property {http://domain.swm.gbicc.net}especialPrompt in class net.gbicc.swm.domain.Bulletin
而事实上Bulletin里并没有especialPrompt属性。。。。这令我很困惑,一度怀疑是Model继承导致的
后来终于找到症结所在——Bulletin里面的一些boolean isxxx方法把 xfire骗了,为他们新建一些对应的setxxx(boolean)就没问题了,绿条如约出现。
整个过程虽然有些艰难,但是其实配置量还是很少的,再看看 xfire替我们生成的一大堆WSDL,也就释然了,谁教咱程序员以“懒惰”为美德呢?