kbmmw 5.0 中的REST 服务

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

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

posted @ 2019-07-01 23:50  业容天  阅读(60)  评论(0编辑  收藏  举报