kbmmw 的HTTPSmartService入门

简介: 前面介绍过kbmmw 中的smartservice. 这个既可以用于kbmmw 的客户端,也可以使用http 访问。 在新版的kbmmw里面,作者加强了http 的支持,我们可以只使用HTTPSmartService ,这样可以省去很多代码,可以更方便、简单的实现REST 服务。

前面介绍过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;

运行结果

 

 

 

后面我们再介绍数据库的操作。

目录
相关文章
|
开发工具 IDE 数据库管理
kbmmw 5.04 发布
增加了一大波功能,消灭了一大堆问题,也肯定引进了一大票BUG.We are happy to announce the release of our latest version of kbmMW. Downloads are readily available for holders of active SAU's from the portal at: https://portal.
1388 0
|
SQL 关系型数据库 网络架构
kbmmw 5.03 发布
版本一小数,功能一大步 We are happy to announce v5.03 of our popular middleware for Delphi and C++Builder. If you like kbmMW, please let others know! Share th...
1168 0
|
消息中间件 SQL 网络架构
kbmmw 5.02发布
5.02.00 May 27 2017 Important notes (changes that may break existing code) ====================================================== * Changed Use class in kbmMWSmartUtils.
1512 0
|
XML 数据格式
kbmmw 5.01 发布
Important notes (changes that may break existing code) ====================================================== * Officially now only supporting XE2 and forward.
1079 0
|
Java 网络架构 前端开发
kbmmw 5.0 中的REST 服务
目前关于REST 服务的话题越来越热,kbmmw 在5.0 里面开始支持rest。今天我就试一下kbmmw 的 rest 服务。闲话少说,开始。 老规矩,放上两个kbmMWServer1和 kbmMWHTTPSysServerTransport1两个控件。
1110 0
|
Web App开发
初识kbmmw 5 中httpsys的支持
前两天kbmmw 发布了5.0 版。里面一个非常令人兴奋的特性就是原生内部支持http.sys. 有关http.sys 的介绍及优势,我就在这里不多说了,大家可以参照一下我以前的文章。 关于http.sys 的最大优势就是web 服务,我今天就以此为例,在kbmmw中建一个使用httpsys的 web server。
1226 0
|
SQL
KBMMW 4.93.10 发布
例行更新,主要是bugfix. 4.93.10 June 4 2016 Important notes (changes that may break existing code) ====================================================== * Fixed compilation for D2009.
849 0
|
XML 流计算 数据格式
KBMMW 4.93.00 发布
可喜可敬,作者非常勤奋,跟上了delphi 10.1 的步伐。 4.93.00 April 26 2016 Important notes (changes that may break existing code) ===============================...
811 0
|
Linux C++ Java
KBMMW 4.92.00 发布
We are happy to announce the release of kbmMW Professional and Enterprise Edition. Yet again kbmMW continues to set the bar for what an n-tier p...
749 0
|
XML JSON 网络性能优化
KBMMW 4.90.00 发布
kbmMW is a portable, highly scalable, high end application server andenterprise architecture integration (EAI) development framework forWin32, .
800 0