小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

圖文并茂——使用xfire編寫webservice,并通過C#調(diào)用 - 莊周夢(mèng)蝶,孰蝶是我...

 nbtymm 2007-03-09
    我沒學(xué)習(xí)過axis系列,一開始學(xué)的就是xfire,myeclipse5.1支持xfire支持的非常棒。這里講解一個(gè)簡(jiǎn)單的例子,
1.首先建立一個(gè)web service工程:
      new_wizard1.gif
new_wizard2.gif

new_wizard3.gif
4.BMP


點(diǎn)擊finish之后,myeclipse自動(dòng)幫你生成services.xml以及web應(yīng)用目錄結(jié)構(gòu),其中的services.xml是你導(dǎo)出服務(wù)的配置文件,注意在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.接下來(lái),現(xiàn)在編寫要導(dǎo)出的pojo類,首先是接口:
package net.rubyeye.webservicedemo;

//Generated by MyEclipse

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

這個(gè)接口我們只提供一個(gè)方法:sayHello(),我們沒有采用JSR181標(biāo)注式的聲明方式,還是采用xml配置文件。然后是實(shí)現(xiàn)類:
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服務(wù)名稱叫做HelloWorldService,接口是IHelloWorldService,實(shí)現(xiàn)類是HelloWorldServiceImpl。注意,其實(shí)我們這三個(gè)步驟可以一步完成,只要直接使用myeclipse的new web service向?qū)Ъ纯?/strong>

new_webservice_wiz1.gif
new_webservice_wiz1_codefirst2.gif


3.然后將此工程部署到tomcat上,通過http://localhost:8081/HelloWorld/services/HelloWorldService?wsdl可以看到生成的wsdl文件。注意,在部署之后,services會(huì)被拷貝到WEB-INF\classes\META-INF\xfire目錄下,xfire會(huì)自動(dòng)搜索此目錄并加載配置文件。我們可以編寫一個(gè)client來(lái)測(cè)試web服務(wù),你也可以點(diǎn)擊myeclipse上的Launch the Web Services來(lái)測(cè)試web服務(wù)

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();
        }

    }
}

執(zhí)行,打?。篽ello,dennis

注意,你也可以使用MyEclipse的new webservice client向?qū)ё詣?dòng)生成client,同時(shí)生成供客戶端調(diào)用的stub類等。

最后,我們?cè)倬帉懸粋€(gè)C#調(diào)用此web service的例子。
1.在vs.net中新建一個(gè)windows 應(yīng)用程序項(xiàng)目,并添加一個(gè)button,和一個(gè)label
c1.BMP
c2.BMP

2.項(xiàng)目菜單——》添加web應(yīng)用,輸入我們要調(diào)用的web服務(wù)的wsdl文件的url,并點(diǎn)擊前往。
c3.BMP


3.添加引用之后,vs.net會(huì)自動(dòng)幫你生成提供給客戶端調(diào)用的stub等,這些文件在名為localhost的命名空間下。此空間下將有一個(gè)類,名為HelloWorldService。最后,在button1的onclick事件中添加代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
    
public partial class Form1 : Form
    {
        localhost.HelloWorldService helloService 
= new localhost.HelloWorldService();
        
public Form1()
        {
            InitializeComponent();
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            label1.Text 
= helloService.sayHello("dennis");
        }
    }
}

我們new一個(gè)
HelloWorldService ,并調(diào)用sayHello方法,將結(jié)果顯示在label上
4.執(zhí)行ctr+F5

c4.BMP


c5.BMP

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多