`
lws0402
  • 浏览: 107242 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Jruby 开发部署 Web Service

    博客分类:
  • ruby
阅读更多
学习了如何用Jruby开发和部署 Web Service,在这里做个简单的总结。

首先,开发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
   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  


注意:这里是重点,普通的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对象作为参数类型,下面这样子使用是错误的



错误案例:

app/models/project.rb

class Project < ActiveRecord::Base

end



test_api.rb

require “路径/project”

class TestApi < ActionWebService::API::Base

api_method :saveProject,

:expects => [{:project=>Project}],

:returns => [:boolean]

end

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

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



project_struct.rb
   1. class ProjectStruct <ActionWebService::Struct  
   2.   
   3.     member :name,    :string  
   4.   
   5.     member :date,     :date  
   6.   
   7.     member :status,    :int  
   8.   
   9. 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 表示该方法没有返回值
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics