学习了如何用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 表示该方法没有返回值
分享到:
相关推荐
本文介绍了如何用JRuby开发Web Service。用JRuby开发Web Service,需要安装ActionWebService。安装完毕之后便可以开始调用及部署了。
jRuby On Rails WEB 2.0
《JRuby on Rails Web 2.0 实用项目》 英文PDF + 源码
JRuby on Rails Web 2.0 Projects
JRuby window X64 开发软件
jruby与java使用开发,听说jruby能是swing的调用变为更简单
JRuby是在著名的Ruby编程语言的基础上的纯Java的一种实现,它是动态的,开源的编程语言,特色是简单,具有很高的开发效率。Ruby具有很好的语法,编程语句读起来很自然,并且很容易编写。这篇文章给你演示如何使用...
Rails彻底加快及简化了Web应用的开发,不过它让人觉得不够成熟,特别是在高端企业级功能方面。另一方面,Java平台及其虚拟机、库和 应用服务器的速度、稳定性和功能方面却一直在提升,现在已被公认为是开发高端...
Deploying with JRuby: Deliver Scalable Web Apps using the JVM
This book introduces you to three JRuby deployment strategies that give you the performance and scalability you need, while you use a language designed for rapid development. Explore common ...
JRuby是用于Java平台的一个Ruby实现。它使您可以通过JVM执行Ruby代码。在JRuby下运行的Ruby代码还可以装载和使用Java库,包括Swing。所以JRuby+Swing的组合,也成为了开发人员的选择之一。
基于JRuby高效简洁的J2EE Web业务层框架技术研究,董敏敏,王成良,为了降低当前J2EE Web开发的复杂性,提高J2EE Web开发的效率,提出了一种基于JRuby高效简洁的J2EE Web业务层框架。使用该业务层框架不仅使J
Hbci4jruby::Web 待办事项:写一个宝石描述安装将此行添加到应用程序的 Gemfile 中: gem 'hbci4jruby-web' 然后执行: $ bundle或者自己安装: $ gem install hbci4jruby-web用法TODO:在这里写使用说明贡献分叉它...
jruby是一个ruby的解析器,可将ruby解析为java文件,jruby作为ruby解析器时刻调用java代码
jruby-src-1.0.zip
jruby-1.3.1.jar包 解决org.jruby.exceptions.JumpException 等报错
本文介绍了JRuby的安装与配置。JRuby的安装需要JRuby以及JDK,本文使用的版本分别是JRuby 1.1.2以及Java 1.6。至于JRuby配置,文章主要介绍了测试JRuby安装的方法。
jruby-complete-1.7.19.jar,java版本的ruby解析器
最近要做一个简单的系统,上面要求摸索一下Jruby,对应的IDE理所当然的选择了Netbeans,这一系列文章会记录下开发过程中遇到的问题、难点与可能的解决方案。Netbeans版本为6.5.1 ruby版(50M+的那个)。本文主要介绍...
org.jruby.rar