如何使用JRuby开发Web Service

发布时间:2021-11-03 13:40:58 作者:小新
来源:亿速云 阅读:107

这篇文章给大家分享的是有关如何使用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

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

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

二、Jruby部署Web Service

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

class TestApi <  ActionWebService::API::Base    api_method :getMessage,                :expects => [{:msg=>:string}],                :returns => [:string]   end

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

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开发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对象作为参数类型,下面这样子使用是错误的

错误案例:

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   class ProjectStruct < ActionWebService::Struct       member :name,    :string      member :date,     :date      member :status,    :int  end

此时,上述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] 表示该方法返回值的类型,这里可以直接用ActiveRecord model作为返回类型

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

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

感谢各位的阅读!关于“如何使用JRuby开发Web Service”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. 学习笔记-Exchange Web Service API-开发
  2. Web Service介绍

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jruby web service

上一篇:ASP.NET控件使用技巧有哪些

下一篇:JS设计模式的六大原则是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》