自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用JRuby開發(fā)Web Service全攻略

開發(fā) 后端
本文介紹了如何用JRuby開發(fā)Web Service。用JRuby開發(fā)Web Service,需要安裝ActionWebService。安裝完畢之后便可以開始調(diào)用及部署了。

學(xué)習(xí)了如何用Jruby開發(fā)Web Service,在這里做個(gè)簡單的總結(jié)。

首先,用JRuby開發(fā)Web Service,需要安裝ActionWebService,由于rails2.0后的版本已經(jīng)去掉了ActionWebService,所以現(xiàn)在官網(wǎng)不再更新ActionWebService,所以要正常的開發(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版本來的,我用的rails版本是2.2.2的。

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

如果安裝成功,現(xiàn)在就可以開發(fā)部署我們的Web Service了。以下是JRuby開發(fā)Web Service全過程:

一、Jruby調(diào)用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 

首先從標(biāo)準(zhǔn)庫載入soap/wsdlDriver,定義一個(gè)wsdl文件的URL,然后通過該URL創(chuàng)建一個(gè)由WSDL描述的Web service的對象,之后就可以調(diào)用webservice中定義的方法startSession(name, password)。

二、Jruby部署Web Service

首先建立一個(gè)名為”test_api.rb的文件”,其內(nèi)容如下:

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

其次,在app/controller下建立一個(gè)名為test_server_controller.rb,其內(nèi)容如下:

  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開發(fā)Web Service的重點(diǎn),普通的controller會繼承自ApplicationController,但是用于發(fā)布Web Service的controller必須要繼承ActionController::Base(網(wǎng)上很多例子都是繼承的ApplicationController,開始學(xué)習(xí)的時(shí)候總不知道是什么原因?qū)е掳l(fā)布不成功,仔細(xì)對比才發(fā)現(xiàn)是controller繼承了錯(cuò)誤的類)。

做到這里,我們已經(jīng)成功的發(fā)布了一個(gè)很簡單的Web Service,打開服務(wù)器,在瀏覽器中通過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ù)的類型是String,如果有兩個(gè)參數(shù)一個(gè)是Sting,一個(gè)是int可以這樣寫expectx => [{:one_parameter=>:string},{:two_parameter=>:int}]

在expects中的類型不能使用ActiveRecord::Base的子類,也就是說expects中不能使用創(chuàng)建的model對象作為參數(shù)類型,下面這樣子使用是錯(cuò)誤的

錯(cuò)誤案例:

  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時(shí)會出現(xiàn)這樣的錯(cuò)誤提示:“ActiveRecord model classes not allowed in :expects”

expects不支持ActiveRecord作為傳入?yún)?shù),但我們可以自己寫一個(gè)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 

此時(shí),上述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 表示調(diào)用該方法不需要傳入?yún)?shù)

忽略 :returns 表示該方法沒有返回值

JRuby開發(fā)Web Service的配置就介紹到這里,希望能對你有所幫助。

【編輯推薦】

  1. 在JRuby下將RoR項(xiàng)目配置到tomcat詳解
  2. 如何在tomcat上配置jruby on rails
  3. JRuby是什么? JRuby語言概覽
  4. JRuby中調(diào)用java帶可變參數(shù)的方法
  5. 使用JRuby生成JVM代碼
責(zé)任編輯:yangsai 來源: CSDN博客
相關(guān)推薦

2013-06-08 11:13:00

Android開發(fā)XML解析

2009-02-20 11:43:22

UNIXfish全攻略

2011-08-09 09:37:49

2013-05-22 10:00:30

iOSWeb Appicon

2024-10-25 15:25:42

2024-05-07 09:01:21

Queue 模塊Python線程安全隊(duì)列

2013-04-15 10:48:16

Xcode ARC詳解iOS ARC使用

2010-04-23 14:04:23

Oracle日期操作

2023-10-13 19:42:00

2017-07-06 17:39:53

JavaScript開發(fā)程序員

2014-03-19 17:22:33

2009-12-14 14:32:38

動(dòng)態(tài)路由配置

2009-10-19 15:20:01

家庭綜合布線

2011-07-19 20:36:56

2021-01-29 17:40:00

Flyme安卓手機(jī)安全

2009-07-20 09:22:36

用jruby調(diào)用jfr

2013-11-13 00:37:12

微信微信公號微信公眾賬號

2011-01-11 14:30:29

企業(yè)內(nèi)網(wǎng)開發(fā)環(huán)境

2009-12-17 16:15:00

CCNA640-810

2010-08-25 14:36:02

DHCP服務(wù)器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號