Jruby开发Web Service

学习了如何用Jruby开发Web Service,在这里做个简单的总结。

首先,用JRuby开发Web Service,需要安装ActionWebService,由于rails2.0后的版本已经去掉了ActionWebService,所以现在官网不再更新ActionWebService,所以要正常的开发,就必须安装datanoise-actionwebservice,安装方法

gem install datanoise-actionwebservice -v='2.2.2' –source http://gems.github.com

之后,在config/environment.rb文件中添加下面这段话

config.gem 'datanoise-actionwebservice', :lib => 'actionwebservice', :version => '2.2.2'

其中版本"2.2.2"是根据所用的rails版本来的,我用的rails版本是2.2.2的。

随后,在命令提示行中键入 "gem list",看datanoise-actionwebservice 2.2.2是否安装成功。

如果安装成功,现在就可以开发部署我们的Web Service了。以下是JRuby开发Web Service全过程:

一、Jruby调用Web Service

 
 
  1. require 'soap/wsdlDriver' 
  2. class ProjectController <  ApplicationController  
  3.   def project  
  4.     wsdlLink = 'http://192.168.1.5:7001/bpm?WSDL';  
  5.     soap_client = SOAP::WSDLDriverFactory.new(wsdlLink).create_rpc_driver  
  6.     @result = soap_client.startSession("eric","eric");  
  7.   end 
  8. end 

首先从标准库载入soap/wsdlDriver,定义一个wsdl文件的URL,然后通过该URL创建一个由WSDL描述的Web service的对象,之后就可以调用webservice中定义的方法startSession(name, password)。

二、Jruby部署Web Service

首先建立一个名为”test_api.rb的文件”,其内容如下:

 
 
  1. class TestApi <  ActionWebService::API::Base  
  2.   api_method :getMessage,   
  3.              :expects => [{:msg=>:string}],   
  4.              :returns => [:string]  
  5.  
  6. end 

其次,在app/controller下建立一个名为test_server_controller.rb,其内容如下:

 
 
  1. require “路径名/test_api”  
  2. class TestServerController <  ActionController::Base  
  3.   web_service_api TestApi  
  4.   web_service_dispatching_mode :direct 
  5.   wsdl_service_name "test" 
  6.  
  7.   def getMessage(msg)  
  8.       return “The server return ”+msg;  
  9.   end 
  10.  
  11. end 

注意:这里是JRuby开发Web Service的重点,普通的controller会继承自ApplicationController,但是用于发布Web Service的controller必须要继承ActionController::Base(网上很多例子都是继承的ApplicationController,开始学习的时候总不知道是什么原因导致发布不成功,仔细对比才发现是controller继承了错误的类)。

做到这里,我们已经成功的发布了一个很简单的Web Service,打开服务器,在浏览器中通过http://127.0.0.1:3000/test_server/wsdl去看wsdl文档吧。

在test_api.rb文件中,api_method :getMessage 定义的是要发布的Web Service中的方法名

:expects => [{:msg=>:string}] 这表示你定义的方法中,有一个叫msg的参数,并且这个参数的类型是String,如果有两个参数一个是Sting,一个是int可以这样写expectx => [{:one_parameter=>:string},{:two_parameter=>:int}]

在expects中的类型不能使用ActiveRecord::Base的子类,也就是说expects中不能使用创建的model对象作为参数类型,下面这样子使用是错误的

错误案例:

 
 
  1. app/models/project.rb  
  2.  
  3. class Project <  ActiveRecord::Base  
  4.  
  5. end 
  6.  
  7. test_api.rb  
  8.  
  9. require “路径/project”  
  10.  
  11. class TestApi <  ActionWebService::API::Base  
  12.  
  13. api_method :saveProject,   
  14.  
  15. :expects => [{:project=>Project}],   
  16.  
  17. :returns => [:boolean]  
  18.  
  19. end 

这样定义,浏览wsdl时会出现这样的错误提示:“ActiveRecord model classes not allowed in :expects”

expects不支持ActiveRecord作为传入参数,但我们可以自己写一个struct,如下:

 
 
  1. project_struct.rb  
  2.  
  3. class ProjectStruct < ActionWebService::Struct  
  4.  
  5.     member :name,    :string 
  6.  
  7.     member :date,     :date 
  8.  
  9.     member :status,    :int 
  10.  
  11. end 

此时,上述test_api.rb文件就可以改成下面这样:

test_api.rb

 
 
  1. require “路径/project_struct”  
  2.  
  3. class TestApi <  ActionWebService::API::Base  
  4.  
  5.   api_method :saveProject,   
  6.              :expects => [{:project=>ProjectStruct}],   
  7.              :returns => [:boolean]  
  8.  
  9. end 

:returns => [:string] 表示该方法返回值的类型,这里可以直接用ActiveRecord model作为返回类型

忽略 :expects 表示调用该方法不需要传入参数

忽略 :returns 表示该方法没有返回值

JRuby开发Web Service的配置就介绍到这里,希望能对你有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值