图文并茂——使用xfire编写webservice

 

图文并茂——使用xfire编写webservice

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-8438768637354676&dt=1198826338984&lmt=1173318500&prev_fmts=728x15_0ads_al_s&format=336x280_as&output=html&correlator=1198826338968&channel=9876870027&pv_ch=9876870027%2B&url=http%3A%2F%2Fwww.duduwolf.com%2Fwiki%2F2007%2F292.html&color_bg=FFFFFF&color_text=000000&color_link=006699&color_url=006699&color_border=FFFFFF&ad_type=text_image&cc=100&ga_vid=846967742.1198738636&ga_sid=1198826339&ga_hid=1846820026&ga_fc=true&flash=9&u_h=960&u_w=1280&u_ah=930&u_aw=1280&u_cd=32&u_tz=480&u_his=5&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency">
    我没学习过axis系列,一开始学的就是xfire,myeclipse5.1支持xfire支持的非常棒。这里讲解一个简单的例子,
1.首先建立一个web service工程:
      new_wizard1.gif
new_wizard2.gif

new_wizard3.gif
4.BMP


点击finish之后,myeclipse自动帮你生成services.xml以及web应用目录结构,其中的services.xml是你导出服务的配置文件,注意在WEB-INF/web.xml文件中配置了xfire自己的servlet.
< servlet >
    
< servlet-name > XFireServlet </ servlet-name >
    
< servlet-class > org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class >
    
< load-on-startup > 0 </ load-on-startup >
  
</ servlet >
  
< servlet-mapping >
    
< servlet-name > XFireServlet </ servlet-name >
    
< url-pattern > /services/* </ url-pattern >
  
</ servlet-mapping >


2.接下来,现在编写要导出的pojo类,首先是接口:
package  net.rubyeye.webservicedemo;

// Generated by MyEclipse

public   interface  IHelloWorldService {
    
    
public  String sayHello(String name);
    
}

这个接口我们只提供一个方法:sayHello(),我们没有采用JSR181标注式的声明方式,还是采用xml配置文件。然后是实现类:
package  net.rubyeye.webservicedemo;

// Generated by MyEclipse

public   class  HelloWorldServiceImpl  implements  IHelloWorldService {
    
    
public  String sayHello(String name){
        
return   " hello, " + name;
    }
    
    
}

最后,配置下services.xml文件:
< service >
        
< name > HelloWorldService </ name >
        
< serviceClass >
            net.rubyeye.webservicedemo.IHelloWorldService
        
</ serviceClass >
        
< implementationClass >
            net.rubyeye.webservicedemo.HelloWorldServiceImpl
        
</ implementationClass >
        
< style > wrapped </ style >
        
< use > literal </ use >
        
< scope > application </ scope >
    
</ service >

我们的web服务名称叫做 HelloWorldService,接口是 IHelloWorldService,实现类是 HelloWorldServiceImpl。注意,其实我们这三个步骤可以一步完成,只要直接使用myeclipse的new web service向导即可

new_webservice_wiz1.gif
new_webservice_wiz1_codefirst2.gif


3.然后将此工程部署到tomcat上,通过http://localhost:8081/HelloWorld/services/HelloWorldService?wsdl可以看到生成的wsdl文件。注意,在部署之后,services会被拷贝到WEB-INF/classes/META-INF/xfire目录下,xfire会自动搜索此目录并加载配置文件。我们可以编写一个client来测试web服务,你也可以点击myeclipse上的Launch the Web Services来测试web服务

webexplorer_launch.gif


4.编写client代码:

package  net.rubyeye.webservicedemo;

import  java.net.MalformedURLException;
import  java.util.ArrayList;
import  java.util.List;

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  HelloWorldClient {
    
public   static   void  main(String args[]) {
        Service srvcModel 
=   new  ObjectServiceFactory()
                .create(IHelloWorldService.
class );
        XFireProxyFactory factory 
=   new  XFireProxyFactory(XFireFactory
                .newInstance().getXFire());
        
        String helloWorldURL 
=   " http://localhost:8081/HelloWorld/services/HelloWorldService " ;
        
try  {
            IHelloWorldService srvc 
=  (IHelloWorldService) factory.create(
                    srvcModel, helloWorldURL);
            System.out.print(srvc.sayHello(
" dennis " ));
        } 
catch  (MalformedURLException e) {
            e.printStackTrace();
        }

    }
}

执行,打印:hello,dennis

注意,你也可以使用MyEclipse的new webservice client向导自动生成client,同时生成供客户端调用的stub类等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值