kbmmw 的HTTPSmartService入門


前面介紹過kbmmw 中的smartservice. 這個既可以用於kbmmw 的客戶端,也可以使用http 訪問。

在新版的kbmmw里面,作者加強了http 的支持,我們可以只使用HTTPSmartService

,這樣可以省去很多代碼,可以更方便、簡單的實現REST 服務。

首先先建一個工程文件,放置對應的控件。

 

 

新建一個kbmmw service

選HTTP smart service ,(這個向導界面太丑了,希望作者以后能請個美工處理一下)。

 

剩下的一路點過去,到最后生成代碼。

回到主窗口,輸入以下對應的代碼

procedure TForm1.Button1Click(Sender: TObject);
begin
 kbmmwserver1.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;

implementation

uses kbmMWExceptions;

{$R *.dfm}


// Service definitions.
//---------------------

function TkbmMWCustomHTTPSmartService1.HelloWorld:string;
begin
     Result:='Hello world';
   
end;

initialization
  TkbmMWRTTI.EnableRTTI(TkbmMWCustomHTTPSmartService1);
end.

由於我們是要做rest,因此,修改一下helloworld 的代碼,使其更符合rest 的格式

function TkbmMWCustomHTTPSmartService1.HelloWorld:string;
begin
     Result:='{"result":"Hello world"}';
end;

ok, 編譯運行,使用瀏覽器訪問

 

 沒有任何問題,非常簡單方便。

 我們可以直接增加新的函數。

     [kbmMW_Rest('method:get, path:version')]
     [kbmMW_Method]
     function version:string;

  end;

implementation

uses kbmMWExceptions;

{$R *.dfm}


// Service definitions.
//---------------------

function TkbmMWCustomHTTPSmartService1.version: string;
begin
      Result:='{"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;
begin
     result:='{"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;
begin
      Result:='{"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;
var
  vl:TkbmMWHTTPCustomValues;
  s,xsxm,sfzh:string;

  p:Tbytes;
 begin
       vl:=TkbmMWHTTPQueryValues.Create;
               try

                p:= RequestStream.SaveToBytes;

                vl.AsString:= Tencoding.ASCII.GetString(p);

                  xsxm:= vl.ValueByName['xsxm'];
                  sfzh:=vl.ValueByName['sfzh'];

                     // 這里就可以向數據庫里面操作了

                   result:='姓名:'+xsxm+'      身份證號'+sfzh;
                   finally

                    vl.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;
var
  vl:TkbmMWHTTPCustomValues;
  s,xsxm,sfzh:string;
begin
     vl:=TkbmMWHTTPQueryValues.Create;
               try


                vl.AsString:= body;
                xsxm:= vl.ValueByName['xsxm'];
                sfzh:=vl.ValueByName['sfzh'];

                     // 這里就可以向數據庫里面寫了


                    result:='姓名:'+xsxm+'      身份證號'+sfzh;
                   finally

                    vl.Free ;
                   end;

         SetResponseMimeType('text/html');

end;

運行結果

 

 

 

后面我們再介紹數據庫的操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM