技术开发 频道

RoR:Ruby On Rails 的 Web Service



【IT168 技术文档】

1.c:\rails flexstore
2.修改数据库配置文件 database.yml
3.c:\flexstore\ruby script/generate model product
4.c:\flexstore\ruby script/generate web_service ProductWervice getall find
5.修改如下两个 rb文件

product_service_api.rb
class ProductServiceApi < ActionWebService::API::Base
  api_method :getall,:returns => [[Product]]
  api_method :find,
  :expects => [{:from=>:int},{:to=>:int}],
  :returns => [[Product]]
end


product_service_controller.rb
class ProductServiceController < ApplicationController
  wsdl_service_name 'ProductService'
  web_service_scaffold :invoke #此处可以像.NET一下直接在浏览器测试web service
  def getall
    return Product.find_all
  end

  def find
    return Product.find(:all,:conditions =>["price >= :from and price <= :to",params])   
   end
end
6. C:\flexstore\ruby script/server

7. http://localhost:3000/Product_Service/service.wsdl

wsdl

<definitions name="ProductService" targetNamespace="urn:ActionWebService"> &#8722; <types> &#8722; <xsd:schema targetNamespace="urn:ActionWebService"> &#8722; <xsd:complexType name="ProductArray"> &#8722; <xsd:complexContent> &#8722; <xsd:restriction base="soapenc:Array"> <xsd:attribute wsdl:arrayType="typens:Product[]" ref="soapenc:arrayType"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType> &#8722; <xsd:complexType name="Product"> &#8722; <xsd:all> <xsd:element name="id" type="xsd:int"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="description" type="xsd:string"/> <xsd:element name="price" type="xsd:double"/> <xsd:element name="image" type="xsd:string"/> <xsd:element name="triband" type="xsd:int"/> <xsd:element name="camera" type="xsd:int"/> <xsd:element name="video" type="xsd:int"/> </xsd:all> </xsd:complexType> </xsd:schema> </types> <message name="Getall"> </message> &#8722; <message name="GetallResponse"> <part name="return" type="typens:ProductArray"/> </message> &#8722; <message name="Find"> <part name="from" type="xsd:int"/> <part name="to" type="xsd:int"/> </message> &#8722; <message name="FindResponse"> <part name="return" type="typens:ProductArray"/> </message> &#8722; <portType name="ProductServiceProductServicePort"> &#8722; <operation name="Getall"> <input message="typens:Getall"/> <output message="typens:GetallResponse"/> </operation> &#8722; <operation name="Find"> <input message="typens:Find"/> <output message="typens:FindResponse"/> </operation> </portType> &#8722; <binding name="ProductServiceProductServiceBinding" type="typens:ProductServiceProductServicePort"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/> &#8722; <operation name="Getall"> <soap:operation soapAction="/product_service/api/Getall"/> &#8722; <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:ActionWebService" use="encoded"/> </input> &#8722; <output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:ActionWebService" use="encoded"/> </output> </operation> &#8722; <operation name="Find"> <soap:operation soapAction="/product_service/api/Find"/> &#8722; <input> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:ActionWebService" use="encoded"/> </input> &#8722; <output> <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:ActionWebService" use="encoded"/> </output> </operation> </binding> &#8722; <service name="ProductServiceService"> &#8722; <port name="ProductServiceProductServicePort" binding="typens:ProductServiceProductServiceBinding"> <soap:address location="http://localhost:3000/product_service/api"/> </port> </service> </definitions>


8.测试 http://localhost:3000/Product_Service/invoke


9.使用自己定义的结构
#model
class ReportStruct < ActionWebService::Struct
  member :id, :int
  member :CreateDate,:string
  member :UpdateDate,:string
  member :user_id,:int
  member :Content,:string
  member :username,:string
end

#Controller
def reports_find(userid)
    if userid != 0
      reports = Report.find(:all,:conditions => ["user_id= :userid",params],:order => "CreateDate desc")
    else
      reports = Report.find(:all,:order => "CreateDate desc")
    end
    results = []
    reports.each do |rec|
       results << ReportStruct.new(:id => rec.id, :CreateDate => rec.CreateDate,
           :UpdateDate => rec.UpdateDate, :user_id => rec.user_id,
           :Content => rec.Content,:username => rec.user.DisplayName)
    end
    return results 
  end

0
相关文章