RoR:Ruby On Rails 的 Web Service

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" >

    
< types >

    
< xsd:schema  targetNamespace ="urn:ActionWebService" >

    
< xsd:complexType  name ="ProductArray" >

    
< xsd:complexContent >

    
< xsd:restriction  base ="soapenc:Array" >
< xsd:attribute  wsdl:arrayType ="typens:Product[]"  ref ="soapenc:arrayType" />
</ xsd:restriction >
</ xsd:complexContent >
</ xsd:complexType >

    
< xsd:complexType  name ="Product" >

    
< 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 >

    
< message  name ="GetallResponse" >
< part  name ="return"  type ="typens:ProductArray" />
</ message >

    
< message  name ="Find" >
< part  name ="from"  type ="xsd:int" />
< part  name ="to"  type ="xsd:int" />
</ message >

    
< message  name ="FindResponse" >
< part  name ="return"  type ="typens:ProductArray" />
</ message >

    
< portType  name ="ProductServiceProductServicePort" >

    
< operation  name ="Getall" >
< input  message ="typens:Getall" />
< output  message ="typens:GetallResponse" />
</ operation >

    
< operation  name ="Find" >
< input  message ="typens:Find" />
< output  message ="typens:FindResponse" />
</ operation >
</ portType >

    
< binding  name ="ProductServiceProductServiceBinding"  type ="typens:ProductServiceProductServicePort" >
< soap:binding  transport ="http://schemas.xmlsoap.org/soap/http"  style ="rpc" />

    
< operation  name ="Getall" >
< soap:operation  soapAction ="/product_service/api/Getall" />

    
< input >
< soap:body  encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/"  namespace ="urn:ActionWebService"  use ="encoded" />
</ input >

    
< output >
< soap:body  encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/"  namespace ="urn:ActionWebService"  use ="encoded" />
</ output >
</ operation >

    
< operation  name ="Find" >
< soap:operation  soapAction ="/product_service/api/Find" />

    
< input >
< soap:body  encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/"  namespace ="urn:ActionWebService"  use ="encoded" />
</ input >

    
< output >
< soap:body  encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/"  namespace ="urn:ActionWebService"  use ="encoded" />
</ output >
</ operation >
</ binding >

    
< service  name ="ProductServiceService" >

    
< 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

转载于:https://www.cnblogs.com/mobile/archive/2006/12/06/584424.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值