使用JRuby開(kāi)發(fā)Web Service全攻略
學(xué)習(xí)了如何用Jruby開(kāi)發(fā)Web Service,在這里做個(gè)簡(jiǎn)單的總結(jié)。
首先,用JRuby開(kāi)發(fā)Web Service,需要安裝ActionWebService,由于rails2.0后的版本已經(jīng)去掉了ActionWebService,所以現(xiàn)在官網(wǎng)不再更新ActionWebService,所以要正常的開(kāi)發(fā),就必須安裝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"是根據(jù)所用的rails版本來(lái)的,我用的rails版本是2.2.2的。
隨后,在命令提示行中鍵入 "gem list",看datanoise-actionwebservice 2.2.2是否安裝成功。
如果安裝成功,現(xiàn)在就可以開(kāi)發(fā)部署我們的Web Service了。以下是JRuby開(kāi)發(fā)Web Service全過(guò)程:
一、Jruby調(diào)用Web Service
- require 'soap/wsdlDriver'
 - class ProjectController < ApplicationController
 - def project
 - wsdlLink = 'http://192.168.1.5:7001/bpm?WSDL';
 - soap_client = SOAP::WSDLDriverFactory.new(wsdlLink).create_rpc_driver
 - @result = soap_client.startSession("eric","eric");
 - end
 - end
 
首先從標(biāo)準(zhǔn)庫(kù)載入soap/wsdlDriver,定義一個(gè)wsdl文件的URL,然后通過(guò)該URL創(chuàng)建一個(gè)由WSDL描述的Web service的對(duì)象,之后就可以調(diào)用webservice中定義的方法startSession(name, password)。
二、Jruby部署Web Service
首先建立一個(gè)名為”test_api.rb的文件”,其內(nèi)容如下:
- class TestApi < ActionWebService::API::Base
 - api_method :getMessage,
 - :expects => [{:msg=>:string}],
 - :returns => [:string]
 - end
 
其次,在app/controller下建立一個(gè)名為test_server_controller.rb,其內(nèi)容如下:
- require “路徑名/test_api”
 - class TestServerController < ActionController::Base
 - web_service_api TestApi
 - web_service_dispatching_mode :direct
 - wsdl_service_name "test"
 - def getMessage(msg)
 - return “The server return ”+msg;
 - end
 - end
 
注意:這里是JRuby開(kāi)發(fā)Web Service的重點(diǎn),普通的controller會(huì)繼承自ApplicationController,但是用于發(fā)布Web Service的controller必須要繼承ActionController::Base(網(wǎng)上很多例子都是繼承的ApplicationController,開(kāi)始學(xué)習(xí)的時(shí)候總不知道是什么原因?qū)е掳l(fā)布不成功,仔細(xì)對(duì)比才發(fā)現(xiàn)是controller繼承了錯(cuò)誤的類(lèi))。
做到這里,我們已經(jīng)成功的發(fā)布了一個(gè)很簡(jiǎn)單的Web Service,打開(kāi)服務(wù)器,在瀏覽器中通過(guò)http://127.0.0.1:3000/test_server/wsdl去看wsdl文檔吧。
在test_api.rb文件中,api_method :getMessage 定義的是要發(fā)布的Web Service中的方法名
:expects => [{:msg=>:string}] 這表示你定義的方法中,有一個(gè)叫msg的參數(shù),并且這個(gè)參數(shù)的類(lèi)型是String,如果有兩個(gè)參數(shù)一個(gè)是Sting,一個(gè)是int可以這樣寫(xiě)expectx => [{:one_parameter=>:string},{:two_parameter=>:int}]
在expects中的類(lèi)型不能使用ActiveRecord::Base的子類(lèi),也就是說(shuō)expects中不能使用創(chuàng)建的model對(duì)象作為參數(shù)類(lèi)型,下面這樣子使用是錯(cuò)誤的
錯(cuò)誤案例:
- 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時(shí)會(huì)出現(xiàn)這樣的錯(cuò)誤提示:“ActiveRecord model classes not allowed in :expects”
expects不支持ActiveRecord作為傳入?yún)?shù),但我們可以自己寫(xiě)一個(gè)struct,如下:
- project_struct.rb
 - class ProjectStruct < ActionWebService::Struct
 - member :name, :string
 - member :date, :date
 - member :status, :int
 - end
 
此時(shí),上述test_api.rb文件就可以改成下面這樣:
test_api.rb
- require “路徑/project_struct”
 - class TestApi < ActionWebService::API::Base
 - api_method :saveProject,
 - :expects => [{:project=>ProjectStruct}],
 - :returns => [:boolean]
 - end
 
:returns => [:string] 表示該方法返回值的類(lèi)型,這里可以直接用ActiveRecord model作為返回類(lèi)型
忽略 :expects 表示調(diào)用該方法不需要傳入?yún)?shù)
忽略 :returns 表示該方法沒(méi)有返回值
JRuby開(kāi)發(fā)Web Service的配置就介紹到這里,希望能對(duì)你有所幫助。
【編輯推薦】















 
 
 




 
 
 
 