庄周梦蝶

生活、程序、未来
   :: 首页 ::  ::  :: 聚合  :: 管理

图文并茂——使用xfire编写webservice,并通过C#调用

Posted on 2007-03-07 18:13 dennis 阅读(11097) 评论(17)  编辑  收藏 所属分类: javaC#历程
    我没学习过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类等。

最后,我们再编写一个C#调用此web service的例子。
1.在vs.net中新建一个windows 应用程序项目,并添加一个button,和一个label
c1.BMP
c2.BMP

2.项目菜单——》添加web应用,输入我们要调用的web服务的wsdl文件的url,并点击前往。
c3.BMP


3.添加引用之后,vs.net会自动帮你生成提供给客户端调用的stub等,这些文件在名为localhost的命名空间下。此空间下将有一个类,名为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一个
HelloWorldService ,并调用sayHello方法,将结果显示在label上
4.执行ctr+F5

c4.BMP


c5.BMP



评论

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-03-08 08:40 by 坏男孩
up!

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-03-08 08:54 by ssss
调用中文的呢?

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-03-08 17:33 by dennis
@ssss
何谓调用中文的呢?

# re: 图文并茂——使用xfire编写webservice,并通过C#调用[未登录]  回复  更多评论   

2007-03-21 15:35 by 无名
谢谢作者无私的奉献,好文章,能帮助初学者知道怎么去开发Web Services

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-03-29 16:45 by zzz
兄弟 我按照你的操作一步一步的来操作的 但是我怎么访问不到wsdl??

3.然后将此工程部署到tomcat上,通过http://localhost:8081/HelloWorld/services/HelloWorldService?wsdl***
走到这步就不行了。。我myeclipse 5.01 tomcat5



HTTP Status 404 - /HellowWorld/services/HelloWorldService

--------------------------------------------------------------------------------

type Status report

message /HellowWorld/services/HelloWorldService

description The requested resource (/HellowWorld/services/HelloWorldService) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/5.0.28

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-03-29 16:58 by dennis
@zzz
/HellowWorld/services/HelloWorldService?wsdl

完整的URL

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-03-29 17:35 by zzz
?????
什么意思
HTTP Status 404 - /HelloWorld/services/HelloWorldService

--------------------------------------------------------------------------------

type Status report

message /HelloWorld/services/HelloWorldService

description The requested resource (/HelloWorld/services/HelloWorldService) is not available.


--------------------------------------------------------------------------------

Apache Tomcat/5.0.28

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-04-04 15:57 by 王洪明
能用java调用webservice吗?

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-04-04 17:25 by dennis
@王洪明
已经演示了,您没看文章吧

# re: 图文并茂——使用xfire编写webservice,并通过C#调用[未登录]  回复  更多评论   

2007-08-14 15:56 by
我怎么看你的东西 看不明白了 ,不能正个特别基础的啊

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2007-09-30 09:11 by Bruce Luo
老大,你直接这样调能行吗?
http://localhost:8081/HelloWorld/services/HelloWorldService这样调,你测试也能通过?
http://localhost:8081/HelloWorld/services/HelloWorldService.ws才
对吧!!!!

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2008-01-17 14:09 by 对方的
楼主,太感谢你了,讲解的非常棒!

# re: 图文并茂——使用xfire编写webservice,并通过C#调用[未登录]  回复  更多评论   

2008-03-05 13:35 by 游子
很不错,介绍的非常详细,使人收益匪浅。再次感谢。

# re: 图文并茂——使用xfire编写webservice,并通过C#调用  回复  更多评论   

2009-07-27 14:51 by yoshinali@126.com
我调用时,出现无法找到方法错误。为什么啊

# re: 楼主你好  回复  更多评论   

2010-11-17 14:46 by 小桂
楼主可以加我QQ么,加的时候注明下Web Service
谢谢哦!

# re: 楼主你好  回复  更多评论   

2010-11-17 14:46 by 小桂
QQ:461624664

# re: 图文并茂——使用xfire编写webservice,并通过C#调用[未登录]  回复  更多评论   

2011-04-24 17:28 by dy
配置server.xml的那一步写错了,

只有注册用户登录后才能发表评论。


网站导航: