xfire学习笔记

 1.新建WEB工程 testservice
 目录如下:
 cn--test--service--impl

 2.要导入的包
       activation-1.0.2.jar
     commons-codec-1.3.jar
     commons-httpclient-3.0.jar
     commons-logging-1.0.4.jar
     jaxen-1.1-beta-8.jar
     jdom-1.0.jar
     log4j-1.2.x.jar
     mail-1.3.3_01.jar
     spring-1.2.x.jar
     stax-api-1.0.jar
     wsdl4j-1.5.2.jar
     wstx-asl-2.9.jar
     xbean-2.1.0.jar
     xbean-spring-2.2.jar
     xfire-all-1.0.jar
     XmlSchema-1.0.jar


 3.数据发布端
 (1)编写发布接口
 package cn.test.service;

 public interface ADService {
  String[][] getDatas();
 }

 (2)编写实现类
 package cn.test.service.impl;

 import cn.test.service.ADService;

 public class ADServiceImpl implements ADService{

  public String[][] getDatas() {
   
   String ads[][] = new String[3][3];
   
   for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
     ads[i][j] = String.valueOf(i * j);
    }
   }
   return ads;
  }

 }
 
  4.增加xfire的配置文件services.xml
    在src目录中加入/src/META-INF/xfire/services.xml
    <beans xmlns="http://xfire.codehaus.org/config/1.0">
 <service>
  <name>AD</name>     <!-- 这个名字比较重要,调用时用的就是它 -->
  <namespace>http://localhost/ADService</namespace>
  <serviceClass>cn.test.service.ADService</serviceClass>
  <implementationClass>
   cn.test.service.impl.ADServiceImpl
  </implementationClass>
  <!-- 这个东西是一个修饰器 貌似调用了SPRING的整合有关 加了会报一个错误
  <inHandlers>
   <handler
    handlerClass="org.codehaus.xfire.demo.handlers.CheckVersionHandler" />
  </inHandlers>
  -->
 </service>
    </beans>
   
 5.配置web.xml
    在web.xml中加入:
         <servlet>
  <servlet-name>XFireServlet</servlet-name>
  <display-name>XFire Servlet</display-name>
  <servlet-class>
   org.codehaus.xfire.transport.http.XFireConfigurableServlet
  </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>XFireServlet</servlet-name>
  <url-pattern>/services/*</url-pattern>
 </servlet-mapping>

 

 6.测试xfire是否部署成功
 http://localhost/testservice/services/AD?wsdl

 
 7.编写客户端
 package cn.test.service;

 import java.net.MalformedURLException;
 import org.codehaus.xfire.XFire;
 import org.codehaus.xfire.XFireFactory;
 import org.codehaus.xfire.client.XFireProxyFactory;
 import org.codehaus.xfire.service.Service;
 import org.codehaus.xfire.service.binding.ObjectServiceFactory;

 public class CallService {
  public String[][] getData(){
      Service serviceModel = new ObjectServiceFactory().create(ADService.class);       //注意,此处用的接口是发布端的接口ADService.class
    
      XFire xfire = XFireFactory.newInstance().getXFire();
      XFireProxyFactory factory = new XFireProxyFactory(xfire);    
    
      String serviceUrl = "http://localhost/testservice/services/AD";
        
      ADService client = null;
      try {
      client = (ADService) factory.create(serviceModel, serviceUrl);
      } catch (MalformedURLException e) {
     System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());
      }  
        
     
      String[][] datas = null ;
      try {
   datas = client.getDatas();
      } catch (Exception e){
   System.out.println("WsClient.callWebService(): EXCEPTION: " + e.toString());
      }      
     
      //将所有的数据逐个打印出来
      int len = datas.length;
      for(int i=0;i<len;i++){
   int len2 = datas[i].length;
   for(int j=0;j<len2;j++){
    System.out.print("/t"+datas[i][j]);
   }
   System.out.println();
      }

      return datas;
  }
 }

  8.写一个JSP调用客户端
 <%@ page language="java" import="java.util.*,cn.yicha.service.*" pageEncoding="utf-8"%>
 
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
   <head>
     <title>My JSP 'index.jsp' starting page</title> 
   </head> 
   <body>
  webservice test!
    
     <%
  CallService cs = new CallService();
  cs.getData();
    
      %>
   </body>
 </html>
 

你可能感兴趣的:(xfire学习笔记)