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