In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use JRuby to develop Web Service. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
First of all, to develop WebService with JRuby, you need to install ActionWebService. Since ActionWebService has been removed from the version after rails2.0, ActionWebService is no longer updated on the official website, so in order to develop normally, you must install datanoise-actionwebservice. Installation method
Gem install datanoise-actionwebservice-vault 2.2.2'- source http://gems.github.com
After that, add the following paragraph to the config/environment.rb file
Config.gem 'datanoise-actionwebservice',: lib = >' actionwebservice',: version = > '2.2.2'
The version "2.2.2" is based on the rails version used, and the rails version I use is 2.2.2.
Then type "gem list" at the command prompt to see if datanoise-actionwebservice 2.2.2 is installed successfully.
If the installation is successful, we can now develop and deploy our Web Service. The following is the whole process of developing Web Service by JRuby:
1. Jruby calls 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
Second, create a test_server_controller.rb under app/controller, which reads as follows:
Require "pathname / 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}] this means that in the method you define, there is a parameter called msg, and the type of this parameter is String. If there are two parameters, one is Sting and the other is int, you can write expectx = > [{: one_parameter= >: string}, {: two_parameter= >: int}]
Types in expects cannot use subclasses of ActiveRecord::Base, that is, the created model object cannot be used as a parameter type in expects. It is wrong to use the following
Error case:
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
By definition, an error message will appear when browsing wsdl: "ActiveRecord model classes not allowed in: expects"
Expects does not support ActiveRecord as an input parameter, but we can write a struct ourselves, as follows:
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] indicates the type of value returned by this method. Here, you can directly use ActiveRecord model as the return type.
Ignore: expects indicates that no parameters are required to call the method
Ignore: returns indicates that the method does not return a value
Thank you for reading! This is the end of this article on "how to use JRuby to develop Web Service". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.