天天看點

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.   <!-- 注意因為servlet-name為xfire,固xfire配置檔案名應該是xfire-servlet.xml -->  
  23.   <servlet>    
  24.        <servlet-name>xfire</servlet-name>    
  25.        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  26.   </servlet>    
  27.   <servlet-mapping>  
  28.        <servlet-name>xfire</servlet-name>  
  29.        <url-pattern>*.ws</url-pattern>  
  30.   </servlet-mapping>  
  31.   <!-- 配合Spring容器中XFire一起工作的Servlet- -->  
  32.   <servlet>  
  33.     <servlet-name>xfireServlet</servlet-name>  
  34.     <servlet-class>  
  35.     org.codehaus.xfire.spring.XFireSpringServlet  
  36.     </servlet-class>  
  37.   </servlet>  
  38.   <servlet-mapping>  
  39.     <servlet-name>xfireServlet</servlet-name>  
  40.     <url-pattern>/services  
  41.     public static void main(String[] args) {  
  42.         ApplicationContext ac=new ClassPathXmlApplicationContext("client.xml");  
  43.         IHello hello=(IHello)ac.getBean("testWebService");  
  44.         hello.sayHello("lubing");  
  45.     }  
  46. }