spring3.0整合Xfire1.2.6 开发webservice

我的web项目使用了spring3.0.3框架,服务器是Tomcat6.0,要将service层的一个接口发布成webservice,使用的是Xfire1.2.6框架,以下是基本步骤:

 

(1) 导入环境所需jar包

 

需要的spring3.0.3的jar包只有一个:org.springframework.web.servlet-3.0.3.RELEASE.jar

xfire-all-1.2.6.jar,xfire的jar包

jdom.jar,

wsdl4j-1.6.1.jar,

com.springsource.org.aopalliance-1.0.0.jar,在3.0.3中没有提供类似的jar

 

(2) 在web-inf目录下新建xfire-servlet.xml文件(根据Spring规范,名字必须为xfire-servlet.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="/UserService">----发布的webservice路径最后部分
                    <ref bean="userService"/>
                </entry>
            </map>
        </property>
    </bean>
   
    <bean id="userService" 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="userBean"/>----在项目中配的service接口实现类的bean
        </property>
        <property name="serviceClass">
            <value>com.***.***.service.inter.UserService</value>-----service接口的包名+类名
        </property>
    </bean>

</beans>

 

(3) 在web.xml中添加以下内容:

 

<context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:org/codehaus/xfire/spring/xfire.xml</param-value>

 </context-param>

 

    <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>/WebService/*</url-pattern>----webservice路径的一部分,该路径会被xfire拦截,所以完整的路径就是http://localhost:8080/项目名/WebService/UserService?wsdl
    </servlet-mapping>

 

(4)启动tomcat,在浏览器中输入http://localhost:8080/项目名/WebService/webservice名?wsdl,如果出现以下内容说明成功:

<?xml version="1.0" encoding="UTF-8" ?>

- < wsdl:definitions targetNamespace =" http://inter.service.ccs.hhl.com " xmlns:soapenc12 =" http://www.w3.org/2003/05/soap-encoding " xmlns:tns =" http://inter.service.ccs.hhl.com " xmlns:wsdl =" http://schemas.xmlsoap.org/wsdl/ " xmlns:xsd =" http://www.w3.org/2001/XMLSchema " xmlns:soap11 =" http://schemas.xmlsoap.org/soap/envelope/ " xmlns:wsdlsoap =" http://schemas.xmlsoap.org/wsdl/soap/ " xmlns:soapenc11 =" http://schemas.xmlsoap.org/soap/encoding/ " xmlns:soap12 =" http://www.w3.org/2003/05/soap-envelope ">
- < wsdl:types >
- < xsd:schema xmlns:xsd =" http://www.w3.org/2001/XMLSchema " attributeFormDefault =" qualified " elementFormDefault =" qualified "
......
  (5)编写客户端
    客户端需要的jar包:commons-httpclient-3.0.jar,commons-codec-1.3.jar,stax-api-1.0.1.jar,wstx-asl-3.2.0.jar
         Service srvcModel = new ObjectServiceFactory().create(UserService.class);
     XFire xfire = XFireFactory.newInstance().getXFire();
     XFireProxyFactory factory = new XFireProxyFactory(xfire);
     String serviceUrl = "http://localhost:8080/项目名/WebService/webservice名";
     UserService hs = null;
      try {
       hs = (UserService)factory.create(srvcModel, serviceUrl);
      } catch (MalformedURLException e) {
       e.printStackTrace();
      }
      hs.hello();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
package com.xfire.core.client; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.xfire.core.entity.UserInfo; import com.xfire.core.service.IUserInfoService; /** *@author jilongliang *@Date 2012-3-5 * */ public class UserInfoClient { public static void main(String[] args) { getServiceList(); setServiceList(); } static String url = "http://localhost:8081/xfire/services/UserInfo"; /** * */ public static void getServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List list = (ArrayList) iAddressService .getAddressList(); System.out.println("一共多少条数据:" + list.size()); for (Iterator iter = list.iterator(); iter.hasNext();) { UserInfo a = iter.next(); System.out.println(a); } } catch (MalformedURLException e) { e.printStackTrace(); } } public static void setServiceList() { Service service = new ObjectServiceFactory() .create(IUserInfoService.class); try { IUserInfoService iAddressService = (IUserInfoService) new XFireProxyFactory() .create(service, url); List listAdd = new ArrayList(); UserInfo address = new UserInfo(); address.setIdentifier(1); address.setCountry("中國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress("廣東陽春"); address.setPostCode("1111111"); address.setExist(false); address.setArrary(new String[] { "22", "23", "24" }); listAdd.add(address); address.setIdentifier(2); address.setCountry("中國"); address.setProivice("廣東省"); address.setCity("陽江"); address.setAddress(

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值