webservice简单例子

最近重新熟悉下webservice知识,以前用的是axis2,现在用另一种方式spring xfire来实现简单的webservice;
xfire和axis
xfire比axis性能高
axis比xfire响应时间短
一、在eclipse下新建项目工程xfire
二、导入基本的jar包
        commons-codec-1.3.jar
        commons-httpclient-3.0.jar
        commons-logging-1.0.4.jar
        jdom-1.0.jar
        jsr181-api.jar
        spring.jar
        wsdl4j-1.6.1.jar
        xfire-all-1.2.6.jar
        XmlSchema-1.4.7.jar
三、web.xml配置
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.   <display-name>xfire</display-name>    
  4.   <!-- 定义装入的spring配置文件 -->  
  5.   <context-param>  
  6.     <param-name>contextConfigLocation</param-name>  
  7.     <param-value>  
  8.      classpath:org/codehaus/xfire/spring/xfire.xml  
  9.      /WEB-INF/xfire-servlet.xml  
  10.     </param-value>  
  11.   </context-param>  
  12.   <!-- 自动装配配置信息 -->  
  13.   <listener>  
  14.      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  15.   </listener>  
  16.   <!-- 它主要负责处理由 JavaBean Introspector 功能而引起的缓存泄露 -->  
  17.     <listener>  
  18.         <listener-class>  
  19.             org.springframework.web.util.IntrospectorCleanupListener  
  20.         </listener-class>  
  21.     </listener>  
  22.       
  23.   <!-- 注意因为servlet-name为xfire,固xfire配置文件名应该是xfire-servlet.xml -->  
  24.   <servlet>    
  25.        <servlet-name>xfire</servlet-name>    
  26.        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  27.   </servlet>    
  28.   <servlet-mapping>  
  29.        <servlet-name>xfire</servlet-name>  
  30.        <url-pattern>*.ws</url-pattern>  
  31.   </servlet-mapping>  
  32.   <!-- 配合Spring容器中XFire一起工作的Servlet- -->  
  33.   <servlet>  
  34.     <servlet-name>xfireServlet</servlet-name>  
  35.     <servlet-class>  
  36.     org.codehaus.xfire.spring.XFireSpringServlet  
  37.     </servlet-class>  
  38.   </servlet>  
  39.   <servlet-mapping>  
  40.     <servlet-name>xfireServlet</servlet-name>  
  41.     <url-pattern>/services/*</url-pattern>  
  42.   </servlet-mapping>  
  43. </web-app>  


四、xfire-servlet.xml
[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4. <!-- basic configuration -->  
  5.     <!-- 若是web.xml已经配置了org/codehaus/xfire/spring/xfire.xml,这里就不需要配置了 -->  
  6.     <!-- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> -->  
  7.     <bean id="baseExporter" class="org.codehaus.xfire.spring.remoting.XFireExporter"  
  8.         lazy-init="false" abstract="true">  
  9.         <property name="serviceFactory" ref="xfire.serviceFactory" />  
  10.         <property name="xfire" ref="xfire" />  
  11.         <property name="outHandlers">  
  12.             <list>  
  13.                 <ref bean="domOutHandler"/>  
  14.                 <ref bean="soapOutHandler"/>  
  15.             </list>  
  16.         </property>  
  17.         <property name="inHandlers">  
  18.             <list>  
  19.                 <ref bean="domInHandler"/>  
  20.             </list>  
  21.         </property>  
  22.     </bean>  
  23.   
  24.     <bean id="domOutHandler" class="org.codehaus.xfire.util.dom.DOMOutHandler" />  
  25.     <bean id="soapOutHandler" class="com.bing.webservice.SoapOutHandler" />  
  26.     <bean id="domInHandler" class="org.codehaus.xfire.util.dom.DOMInHandler" />  
  27.       
  28.     <!-- basic configuration -->  
  29.       
  30.     <!--  定义访问的url,但是web.xml需要配置org.springframework.web.servlet.DispatcherServlet-->  
  31.     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  32.                <property name="urlMap">  
  33.                    <map>  
  34.                       <entry key="/hello.ws">  
  35.                           <ref bean="helloExporter"/>  
  36.                       </entry>  
  37.                    </map>  
  38.                </property>  
  39.     </bean>  
  40. <!-- hello webservice -->  
  41.      <bean id="helloExporter" parent="baseExporter">  
  42.         <property name="serviceBean" ref="hello" />  
  43.         <property name="serviceClass" value="com.bing.webservice.IHello" />  
  44.         <property name="name" value="sayHello"/>  
  45.     </bean>  
  46.     <bean id="hello" class="com.bing.webservice.HelloImpl" />  
  47.     <!-- hello webservice -->  
  48. </beans>  


五、

接口
[java]  view plain copy
  1. package com.bing.webservice;  
  2.   
  3. public interface IHello {  
  4.     public String sayHello(String name);  
  5. }  



实现类
[java]  view plain copy
  1. package com.bing.webservice;  
  2. public class HelloImpl implements IHello {  
  3.   
  4.     public String sayHello(String name) {  
  5.         return "hello! "+ name;  
  6.     }  
  7.   
  8. }  




拦截器
[java]  view plain copy
  1. package com.bing.webservice;  
  2. import org.codehaus.xfire.MessageContext;  
  3. import org.codehaus.xfire.handler.AbstractHandler;  
  4.   
  5. public class SoapOutHandler extends AbstractHandler {  
  6.   
  7.     public void invoke(MessageContext mc) throws Exception {  
  8.           System.out.println(mc.getCurrentMessage());  
  9.     }  
  10. }  



客户端测试
[java]  view plain copy
  1. package com.bing.webservice;  
  2. import java.net.MalformedURLException;  
  3. import org.codehaus.xfire.XFire;  
  4. import org.codehaus.xfire.XFireFactory;  
  5. import org.codehaus.xfire.client.XFireProxyFactory;  
  6. import org.codehaus.xfire.service.Service;  
  7. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  8.   
  9. public class TestClient {  
  10.     public static void main(String[] args) {  
  11.         Service serviceModel = new ObjectServiceFactory().create(IHello.class);  
  12.         XFire xfire=XFireFactory.newInstance().getXFire();  
  13.         XFireProxyFactory factory=new XFireProxyFactory(xfire);  
  14.         String serviceUrl = "http://localhost:8080/xfire/services/sayHello";  
  15.           IHello ds= null;  
  16.           try {  
  17.            ds=(IHello)factory.create(serviceModel, serviceUrl);  
  18.           } catch (MalformedURLException e) {  
  19.            e.printStackTrace();  
  20.           }  
  21.           System.out.println(ds.sayHello("Tom"));  
  22.     }  
  23.   
  24. }  



另一种方式调用:

client.xml  放在src目录下

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.         <bean id ="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">  
  5.                <property name="serviceClass">  
  6.                    <value>com.bing.webservice.IHello</value>  
  7.                </property>  
  8.                <property name="wsdlDocumentUrl">  
  9.                    <value>  
  10.                     http://localhost:8080/xfire/services/sayHello?wsdl  
  11.                     </value>        
  12.                </property>  
  13.         </bean>  
  14. </beans>  

TestClientByXml.java
[java]  view plain copy
  1. package com.bing.webservice;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class TestClientByXml {  
  7.   
  8.     /** 
  9.      */  
  10.     public static void main(String[] args) {  
  11.         ApplicationContext ac=new ClassPathXmlApplicationContext("client.xml");  
  12.         IHello hello=(IHello)ac.getBean("testWebService");  
  13.         hello.sayHello("lubing");  
  14.   
  15.     }  
  16.   
  17. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值