kbmmw 5.0 中的REST 服务

简介: 目前关于REST 服务的话题越来越热,kbmmw 在5.0 里面开始支持rest。今天我就试一下kbmmw 的 rest 服务。闲话少说,开始。 老规矩,放上两个kbmMWServer1和 kbmMWHTTPSysServerTransport1两个控件。

目前关于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 显示库结合呢?

这个问题就留给各位同学研究吧。

 

目录
相关文章
|
数据库 网络架构
kbmmw 的HTTPSmartService入门
前面介绍过kbmmw 中的smartservice. 这个既可以用于kbmmw 的客户端,也可以使用http 访问。 在新版的kbmmw里面,作者加强了http 的支持,我们可以只使用HTTPSmartService ,这样可以省去很多代码,可以更方便、简单的实现REST 服务。
1813 0
|
开发工具 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.
1408 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...
1197 0
|
Web App开发 API 网络架构
[解读REST] 2.REST用来干什么的?
衔接上文[解读REST] 1.REST的起源,介绍了REST的诞生背景。每当笔者遇到一个新事物的想去了解的时候,总是会问上自己第一个问题,这个新事物是干什么用的?在解释我所理解的REST这个过程中也不例外,这篇博客我们先关注一下REST是干什么用的,然后后续再解释REST是什么。
1505 0
|
消息中间件 SQL 网络架构
kbmmw 5.02发布
5.02.00 May 27 2017 Important notes (changes that may break existing code) ====================================================== * Changed Use class in kbmMWSmartUtils.
1550 0
|
XML 数据格式
kbmmw 5.01 发布
Important notes (changes that may break existing code) ====================================================== * Officially now only supporting XE2 and forward.
1107 0
|
Web App开发
初识kbmmw 5 中httpsys的支持
前两天kbmmw 发布了5.0 版。里面一个非常令人兴奋的特性就是原生内部支持http.sys. 有关http.sys 的介绍及优势,我就在这里不多说了,大家可以参照一下我以前的文章。 关于http.sys 的最大优势就是web 服务,我今天就以此为例,在kbmmw中建一个使用httpsys的 web server。
1249 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.
875 0
|
XML 流计算 数据格式
KBMMW 4.93.00 发布
可喜可敬,作者非常勤奋,跟上了delphi 10.1 的步伐。 4.93.00 April 26 2016 Important notes (changes that may break existing code) ===============================...
840 0