目前關於REST 服務的話題越來越熱,kbmmw 在5.0 里面開始支持rest。今天我就試一下kbmmw 的
rest 服務。閑話少說,開始。
老規矩,放上兩個kbmMWServer1和 kbmMWHTTPSysServerTransport1兩個控件。
設置kbmMWHTTPSysServerTransport1的server 屬性。urls 屬性默認是http://+:80/, 我們在這里就不改了。
因為我們后面采用的是samrtservice. 因此現在在主窗體里面不用再操心后面有什么服務要注冊了。只需要一句話就
ok了。
procedure TForm2.Button1Click(Sender: TObject); begin kbmMWServer1.Active:=True; end; procedure TForm2.FormCreate(Sender: TObject); begin kbmMWServer1.AutoRegisterServices; end;
主窗體就ok 了。
接下來我們來建服務模塊
選擇這個smartservice
記住這里要填成你定義的這個服務名。然后一路點過去。
默認生成的代碼如下:
type [kbmMW_Service('name:xalionservice, flags:[listed]')] [kbmMW_Rest('path:/xalionservice')] // Access to the service can be limited using the [kbmMW_Auth..] attribute. // [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')] TkbmMWCustomSmartService1 = class(TkbmMWCustomSmartService) private { Private declarations } protected { Protected declarations } public { Public declarations } // HelloWorld function callable from both a regular client, // due to the optional [kbmMW_Method] attribute, // and from a REST client due to the optional [kbmMW_Rest] attribute. // The access path to the function from a REST client (like a browser)+ // is in this case relative to the services path. // In this example: http://.../xalionservice/helloworld // Access to the function can be limited using the [kbmMW_Auth..] attribute. // [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')] [kbmMW_Rest('method:get, path:helloworld')] [kbmMW_Method] function HelloWorld:string; end; implementation uses kbmMWExceptions; {$R *.dfm} // Service definitions. //--------------------- function TkbmMWCustomSmartService1.HelloWorld:string; begin Result:='Hello world'; end; initialization TkbmMWRTTI.EnableRTTI(TkbmMWCustomSmartService1); end.
這個代碼比較簡單,只是定義了很少的屬性。
但是已經可以運行了。
直接在瀏覽器里面輸入http://127.0.0.1/xalionservice/helloworld 就可以看到下圖
好,最簡單的rest 服務做好了,我們繼續做更復雜的。
我們加一個輸入字符串,然后回應
[kbmMW_Method('EchoString')] // 回應輸入的串 [kbmMW_Rest('method:get, path: [ "echostring/{AString}","myechostring/{AString}" ]')] [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')] function EchoString([kbmMW_Rest('value: "{AString}"')] const AString:string):string; end; implementation uses kbmMWExceptions; {$R *.dfm} // Service definitions. //--------------------- function TkbmMWCustomSmartService1.EchoString(const AString: string): string; begin result:='你好!'+astring; end;
在瀏覽器里面輸入http://127.0.0.1/xalionservice/echostring/xalion
和我們想象的一樣。
繼續加入更復雜的
[kbmMW_Method] [kbmMW_Rest('method:get, path: "cal/addnumbers"')] function AddNumbers([kbmMW_Rest('value: "$arg1", required: true')] const AValue1:integer; [kbmMW_Rest('value: "$arg2", required: true')] const AValue2:integer; [kbmMW_Arg(mwatRemoteLocation)] const ARemoteLocation:string):integer; end; implementation uses kbmMWExceptions; {$R *.dfm} // Service definitions. //--------------------- function TkbmMWCustomSmartService1.AddNumbers(const AValue1, AValue2: integer; const ARemoteLocation: string): integer; begin Result:=AValue1+AValue2; end;
瀏覽器里面可以輸入http://127.0.0.1/xalionservice/cal/addnumbers?arg1=10&arg2=50
很簡單吧.
下面再說一下,服務屬性的常用參數,大家可以根據自己的需要改。
// server (optional) indicates name of TkbmMWServer instance to register service with. If missing will be registered with all server instances.
// name (optional) overrides service preferred name.
// version (optional) overrides service version.
// minInstances (optional) overrides services minInstances.
// maxInstances (optional) overrides services maxInstances.
// flags (optional). Array that can contain: [ listed,runrequireauth,listrequireauth,stateful,persistent,default ]
// gatherStatistics (optional). Boolean value that can be on/off or true/false.
// maxIdleTime (optional). Integer indicating max idle time in seconds before non stateful service instance is GC'ed.
// maxIdleStatefulTime (optional). Integer indicating max idle time in seconds before stateful service instance is GC'ed.
// timeout (optional). Integer indicating max allowed time of a request in seconds before service instance is GC'ed.
// dontQueue (optional). Boolean indicating if requests should be queued or not if no instances of the service is available at time of request.
[kbmMW_Service('name:SMARTDEMO, version:1.0, minInstances:32, maxInstances:128')]
上面做完了,那么如何通過這個REST 服務與前端的JS 顯示庫結合呢?
這個問題就留給各位同學研究吧。