kbmmw 的HTTPSmartService入门
前面介紹過kbmmw 中的smartservice. 這個既可以用于kbmmw 的客戶端,也可以使用http 訪問。
在新版的kbmmw里面,作者加強了http 的支持,我們可以只使用HTTPSmartService
,這樣可以省去很多代碼,可以更方便、簡單的實現REST 服務。
首先先建一個工程文件,放置對應的控件。
?
?
新建一個kbmmw service
選HTTP smart service ,(這個向導界面太丑了,希望作者以后能請個美工處理一下)。
?
剩下的一路點過去,到最后生成代碼。
回到主窗口,輸入以下對應的代碼
procedure TForm1.Button1Click(Sender: TObject); beginkbmmwserver1.Active:=True; end;procedure TForm1.FormCreate(Sender: TObject); begin kbmmwserver1.AutoRegisterServices; end;再看看生成的代碼
[kbmMW_Service('name:xalionrest, flags:[listed]')][kbmMW_Rest('path:/xalionrest')]// Access to the service can be limited using the [kbmMW_Auth..] attribute.// [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')] TkbmMWCustomHTTPSmartService1 = class(TkbmMWCustomHTTPSmartService)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://.../xalionhttp/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;implementationuses kbmMWExceptions;{$R *.dfm}// Service definitions. //---------------------function TkbmMWCustomHTTPSmartService1.HelloWorld:string; beginResult:='Hello world'; end;initializationTkbmMWRTTI.EnableRTTI(TkbmMWCustomHTTPSmartService1); end.由于我們是要做rest,因此,修改一下helloworld 的代碼,使其更符合rest 的格式
function TkbmMWCustomHTTPSmartService1.HelloWorld:string; beginResult:='{"result":"Hello world"}'; end;ok, 編譯運行,使用瀏覽器訪問
?
?沒有任何問題,非常簡單方便。
?我們可以直接增加新的函數。
[kbmMW_Rest('method:get, path:version')][kbmMW_Method]function version:string;end;implementationuses kbmMWExceptions;{$R *.dfm}// Service definitions. //---------------------function TkbmMWCustomHTTPSmartService1.version: string; beginResult:='{"result":"'+self.Server.Version+'"}'; end;運行顯示
處理參數
如果只有一個參數,可以直接使用 http://127.0.0.1/xalionrest/echostring/{參數}
[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; function TkbmMWCustomHTTPSmartService1.EchoString(const AString: string): string; beginresult:='{"result":"你好!'+astring+'"}';; end;如果是多個參數,可以使用http://127.0.0.1/xalionrest/cal/numbers?arg1=10&arg2=20
[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):string; function TkbmMWCustomHTTPSmartService1.AddNumbers(const AValue1,AValue2: integer; const ARemoteLocation: string):string; beginResult:='{"result":"'+(AValue1+AValue2).ToString+'"}';; end;運行結果
?
下面處理一下post 的函數,首先做一個含有form 的html 文件
<table width="770" border="0" align="center" cellpadding="0" cellspacing="0" class="unnamed2"><tr><td width="848" align="center"><span class="style1">新生錄取查詢</span><br></td></tr><form name="form1" method="post" action="/xalionrest/postdata"><tr><td align="center"><span class="style2">姓名:</span> <input name="xsxm" type="text" id="xsxm"><span class="style2">身份證號:</span> <input name="sfzh" type="text" id="sfzh"></td></tr><tr><td align="center"><br><input type="submit" name="Submit" value="提交" onClick="return B1_onclick()"> <input type="reset" name="Submit" value="重置"></td></tr></form> </table>?
?
對應的函數為
?
[kbmMW_Rest('method:post, path:postdata')][kbmMW_Method]function postdata:string; function TkbmMWCustomHTTPSmartService1.postdata: string; varvl:TkbmMWHTTPCustomValues;s,xsxm,sfzh:string;p:Tbytes;beginvl:=TkbmMWHTTPQueryValues.Create;tryp:= RequestStream.SaveToBytes;vl.AsString:= Tencoding.ASCII.GetString(p);xsxm:= vl.ValueByName['xsxm'];sfzh:=vl.ValueByName['sfzh'];// 這里就可以向數據庫里面操作了 result:='姓名:'+xsxm+' 身份證號'+sfzh;finallyvl.Free ;end;SetResponseMimeType('text/html');end;運行結果
?
?基本上就是這樣。
kbmmw 5.04.40 新增加了post 內容文本自動識別功能,上面的函數可以變得更簡單。
[kbmMW_Rest('method:post, path:poststring')][kbmMW_Method]function poststring([kbmMW_Rest('value: "body", required: true')] const body:string):string; function TkbmMWCustomHTTPSmartService1.poststring(const body: string): string; varvl:TkbmMWHTTPCustomValues;s,xsxm,sfzh:string; beginvl:=TkbmMWHTTPQueryValues.Create;tryvl.AsString:= body;xsxm:= vl.ValueByName['xsxm'];sfzh:=vl.ValueByName['sfzh'];// 這里就可以向數據庫里面寫了 result:='姓名:'+xsxm+' 身份證號'+sfzh;finallyvl.Free ;end;SetResponseMimeType('text/html');end;運行結果
?
?
?
后面我們再介紹數據庫的操作。
轉載于:https://www.cnblogs.com/xalion/p/7895179.html
總結
以上是生活随笔為你收集整理的kbmmw 的HTTPSmartService入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Objective-C字符串处理的函数
- 下一篇: 解决Java工程URL路径中含有中文的情