红鱼儿

使用kbmMW#1轻松实现REST

使用kbmMW很容易创建REST服务器。

首先,我们制作服务器应用程序(或服务......取决于您)。

在这种情况下,我们将添加一个简单的Form,为我们的kbmMW组件提供GUI和位置。

在Delphi中,单击文件 - 新建 - VCL Forms应用程序

拖放以下两个组件到Form上:

  • TkbmMWServer
  • TkbmMWTCPIPIndyServerTransport

 

kbmMWTCPIPIndyServerTransport1的Server属性设置kbmMWServer1

双击kbmMWTCPIPIndyServerTransport1Bindings属性以打开其编辑器。为0.0.0.0端口80添加绑定,这是默认的HTTP服务器端口。您可以选择所需的任何端口其他绑定,但让要REST用户知道。

 

设置kbmMWTCPIPIndyTransport1属性Streamformat为REST(译者注:这是必须的一步,表明服务端可以接收REST请求)

 

现在保存项目,然后双击Form,写OnCreate事件:

procedure TForm7.FormCreate(Sender: TObject);
begin
       kbmMWServer1.AutoRegisterServices;//注册Rest服务
       kbmMWServer1.Active:=true;//启动Server
end;

在接口Uses部分,手工填加单元:kbmMWRESTTransStream。然后保存项目,现在你有了下面的单元代码:

unit Unit7;
 
interface
 
uses
 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, kbmMWCustomTransport, kbmMWServer,
 kbmMWTCPIPIndyServerTransport, kbmMWRESTTransStream;//kbmMWRESTTransStream手工填加的
 
type
 TForm7 = class(TForm)
 kbmMWServer1: TkbmMWServer;
 kbmMWTCPIPIndyServerTransport1: TkbmMWTCPIPIndyServerTransport;
 procedure FormCreate(Sender: TObject);
 private
 { Private declarations }
 public
 { Public declarations }
 end;
 
var
 Form7: TForm7;
 
implementation
 
{$R *.dfm}
 
procedure TForm7.FormCreate(Sender: TObject);
begin
 kbmMWServer1.AutoRegisterServices;
 kbmMWServer1.Active:=true;
end;
 
end.

至此,我们拥有了支持REST访问的Web服务器的基础。现在继续添加可以从任何REST客户端调用的功能。

在Delphi中,单击文件 - 新建 - 其他 - Components4Developers向导,然后选择kbmMW服务向导单击确定。

 

在继续选择我们将添加的kbmMW服务类型之前,我们需要确定要创建的REST服务器类型。它可以是纯REST服务器,只通过代码返回数据,也可以是常规Web服务器,从磁盘上的文件提供数据,如html模板,图像,CSS文件等。这里只想制作一个纯REST服务器,因此我们选择Smart Service / kbmMW_1.0

单击下一步,输入REST服务应该被称为的默认名称。在这个例子中,我称之为MyREST。

单击“下一步”直接进入此页面,然后单击绿色复选标记按钮,生成一个空的服务单元。

从表面上看,它看起来像一个普通的TDataModule,因此可以包含任何可以放在TDataModule上的组件。但是现在我们对它的代码更感兴趣。按F12切换到代码视图。浏览顶部的解释性说明,直到找到实际代码。

type
 
[kbmMW_Service('name:MyREST, flags:[listed]')]
[kbmMW_Rest('path:/MyREST')]
 // Access to the service can be limited using the [kbmMW_Auth..] attribute.
 // [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')]
 
TkbmMWCustomSmartService8 = 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://.../MyREST/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 TkbmMWCustomSmartService8.HelloWorld:string;
begin
 Result:='Hello world';
end;

现在编译并运行应用服务器,就可以访问HelloWorld方法,并得到这个方法返回的内容。找开浏览器,在地址栏输入:

HTTP://LocalHost/MyREST/HelloWorld

确保大小写是正确的,因为HTTP标准描述了地址的URL部分必须区分大小写。如果您要编写http://localhost/MyREST/HelloWorld,则会被告知请求无效。

这一切都很好......但我的REST客户端希望收到一个JSON对象,而不仅仅是简单的文本。

好的..我将展示3种方法......手动的方式,半自动化的方式和完全自动化的方式。

手动方式。将HelloWorld函数更改为如下所示:

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

REST客户端将收到一个匿名对象,其中包含名为result的属性,其中包含“Hello world”。

半自动化方式:

uses kbmMWExceptions
 ,kbmMWObjectNotation
 ,kbmMWJSON;
 
{$R *.dfm}
 
 
// Service definitions.
//---------------------
 
function TkbmMWCustomSmartService8.HelloWorld:string;
var
 o:TkbmMWONObject;
 jsonstreamer:TkbmMWJSONStreamer;
begin
 o:=TkbmMWONObject.Create;
 jsonstreamer:=TkbmMWJSONStreamer.Create;
 
 o.AsString['result']:='Hello world';
 Result:=jsonstreamer.SaveToUTF16String(o);
 
 jsonstreamer.Free;
 o.Free;
end;

这使您可以非常轻松地创建复杂的JSON文档。很酷的部分是,因为我们使用kbmMW的对象表示法框架,我们可以选择将其作为XML或YAML或BSON或MessagePack流式传输,而不是简单地实例化适当的流式传输器。

自动化方式:

type
 
TMyResult = class
private
 FResult:string;
public
 property Result:string read FResult write FResult;
end;
 
 [kbmMW_Service('name:MyREST, flags:[listed]')]
 [kbmMW_Rest('path:/MyREST')]
 // Access to the service can be limited using the [kbmMW_Auth..] attribute.
 // [kbmMW_Auth('role:[SomeRole,SomeOtherRole], grant:true')]
 
TkbmMWCustomSmartService8 = class(TkbmMWCustomSmartService)
 private
 [kbmMW_Rest('method:get, path:helloworld, anonymousResult:true')]
 [kbmMW_Method]
 function HelloWorld:TMyResult;
 end;
 
implementation
 
uses kbmMWExceptions;
 
{$R *.dfm}
 
// Service definitions.
//---------------------
 
function TkbmMWCustomSmartService8.HelloWorld:TMyResult;
begin
 Result:=TMyResult.Create;
 Result.Result:='Hello world';
end;
 
initialization
 TkbmMWRTTI.EnableRTTI(TkbmMWCustomSmartService8);
 kbmMWRegisterKnownClasses([TMyResult]);
end.

自动化方式仅仅意味着返回具有所需信息的对象。kbmMW会自动将对象转换为JSON(因为我们使用的是REST streamformat)。

为了确保kbmMW知道对象类型,我们通过kbmMWRegisterKnownClasses注册它如果不注册,kbmMW会产生异常,表示它不知道该对象。

不用担心TMyResult实例会产生内存泄露,kbmMW会自动释放它。如果你不希望kbmMW这么干,可以通过在HelloWorld方法kbmMW_Rest属性中设置freeResult:false

另外需要注意,kbmMW_Rest属性现在还包含  anonymousResult:true

这告诉kbmMW,希望生成的JSON是匿名的。如果我们没有包含该属性设置,结果将如下所示:

    
{"TMyResult":{"Result":"Hello world"}}

这不一定是错的,但你要知道结果是不同的。

通过在kbmMW_Rest中设置各种属性,来控制返回结果,例如:可以选择以不同的名称返回Result属性等。

kbmMW还能够返回TkbmMemTable实例、数组和许多其他类型的信息,因此很容易通过几乎没有其他代码行来REST化您的kbmMW业务功能。

最后需要说明的是:由于HelloWorld方法也使用属性[kbmMW_Method]标记,表示kbmMWSimpleClient也可以调用这个方法。

 

posted on 2018-07-17 19:31  红鱼儿  阅读(1361)  评论(0编辑  收藏  举报