微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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</>    
  4.   <!-- 定义装入的spring配置文件 -->  
  5. context-param>  
  6.     param-name>contextConfigLocation     param-value      classpath:org/codehaus/xfire/spring/xfire.xml  
  7.      /WEB-INF/xfire-servlet.xml  
  8. <!-- 自动装配配置信息 -->  
  9. listener      listener-class>org.springframework.web.context.ContextLoaderListener<!-- 它主要负责处理由 JavaBean Introspector 功能而引起的缓存泄露 -->  
  10.                      org.springframework.web.util.IntrospectorCleanupListener  
  11.       
  12.   <!-- 注意因为servlet-name为xfire,固xfire配置文件名应该是xfire-servlet.xml -->  
  13.   servlet>    
  14.        servlet-name        servlet-class>org.springframework.web.servlet.dispatcherServletservlet-mappingurl-pattern>*.ws<!-- 配合spring容器中XFire一起工作的Servlet- -->  
  15. >xfireServlet     org.codehaus.xfire.spring.XFireSpringServlet  
  16. >/services/*web-app>  


四、xfire-servlet.xml
copy
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"beans <!-- basic configuration -->  
  1.     <!-- 若是web.xml已经配置了org/codehaus/xfire/spring/xfire.xml,这里就不需要配置了 -->  
  2.     <!-- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> -->  
  3. bean id="baseExporter" class="org.codehaus.xfire.spring.remoting.XFireExporter"  
  4.         lazy-init="false" abstract="true"         property name="serviceFactory" ref="xfire.serviceFactory" />  
  5. property name="xfire" ref="xfire" />  
  6. property name="outHandlers"             list                 ref bean="domOutHandler"                 ref bean="soapOutHandler"             propertyproperty name="inHandlers"ref bean="domInHandler"bean   
  7. bean id="domOutHandler" class="org.codehaus.xfire.util.dom.DOMOutHandler" bean id="soapOutHandler" class="com.bing.webservice.soapOutHandler" bean id="domInHandler" class="org.codehaus.xfire.util.dom.DOMInHandler"        
  8. <!--  定义访问的url,但是web.xml需要配置org.springframework.web.servlet.dispatcherServlet-->  
  9. bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"                property name="urlMap"                    map                       entry key="/hello.ws"                           ref bean="helloExporter"entry<!-- hello webservice -->  
  10.      bean id="helloExporter" parent="baseExporter"property name="serviceBean" ref="hello" property name="serviceClass" value="com.bing.webservice.IHello" property name="name" value="sayHello"bean id="hello" class="com.bing.webservice.HelloImpl" <!-- hello webservice -->  
  11. 五、
    接口
    [java]  copy
    package com.bing.webservice;  
  1.   
  2. public interface IHello {  
  3.     public String sayHello(String name);  
  4. }  

实现类
copy
    class HelloImpl implements IHello {  
  1. public String sayHello(String name) {  
  2.         return "hello! "+ name;  
  3.     }  
  4. }  

拦截
copy
    import org.codehaus.xfire.MessageContext;  
  1. import org.codehaus.xfire.handler.AbstractHandler;  
  2. class SoapOutHandler extends AbstractHandler {  
  3.     void invoke(MessageContext mc) throws Exception {  
  4.           System.out.println(mc.getCurrentMessage());  
  5.     }  
  6. 客户端测试
    copy
      import java.net.MalformedURLException;  
    1. import org.codehaus.xfire.XFire;  
    2. import org.codehaus.xfire.XFireFactory;  
    3. import org.codehaus.xfire.client.XFireProxyFactory;  
    4. import org.codehaus.xfire.service.Service;  
    5. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
    6. class TestClient {  
    7. static void main(String[] args) {  
    8.         Service serviceModel = new ObjectServiceFactory().create(IHello.class);  
    9.         XFire xfire=XFireFactory.newInstance().getXFire();  
    10.         XFireProxyFactory factory=new XFireProxyFactory(xfire);  
    11.         String serviceUrl = "http://localhost:8080/xfire/services/sayHello";  
    12.           IHello ds= null;  
    13.           try {  
    14.            ds=(IHello)factory.create(serviceModel, serviceUrl);  
    15.           } catch (MalformedURLException e) {  
    16.            e.printstacktrace();  
    17.           }  
    18.           System.out.println(ds.sayHello("Tom"));  
    19. 另一种方式调用

      client.xml  放在src目录下

      copy @H_502_1005@
        bean id ="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientfactorybean"                property name="serviceClass"                    value>com.bing.webservice.IHelloproperty name="wsdlDocumentUrl"                     http://localhost:8080/xfire/services/sayHello?wsdl  
      1.                     >        
      2. TestClientByXml.java
        copy
          import org.springframework.context.ApplicationContext;  
        1. import org.springframework.context.support.ClasspathXmlApplicationContext;  
        2. class TestClientByXml {  
        3.     /** 
        4.      */  
        5.         ApplicationContext ac=new ClasspathXmlApplicationContext("client.xml");  
        6.         IHello hello=(IHello)ac.getBean("testWebService");  
        7.         hello.sayHello("lubing");  
        8. }  

        版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

        相关推荐