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();

你可能感兴趣的:(spring,bean,webservice,service,SOAP,encoding)