使用新版kbmMW开发的 smart service,也可以轻松的发布为通过REST来调用的功能。
 
一个 kbmMW smart service象下面这样实现,就可以使用REST来访问:
 
type
 
  // This service is known to smart clients as SMARTDEMO
  // Methods registered for REST access will, if they
  // provide a relative path, be sub pathed under /myserver
  [kbmMW_Service('SMARTDEMO')]
  [kbmMW_Rest('path:/myserver')]
  TkbmMWCustomService2 = class(TkbmMWCustomSmartService)
  public
     // This method can be called both from REST and
     // from smart clients.
     // Its called from REST clients like this:
     // http://../helloworld
     [kbmMW_Method('HelloWorld')]
     [kbmMW_Rest('method:get, path: "/helloworld"')]
     function HelloWorld:string;
 
 
     // This method can be called both from REST and
     // from smart clients.
     // Its called from REST clients like this:
     // http://../myserver/myechostring/somestring
     // or
     // http://../myserver/echostring/somestring
     // somestring (can be anything) will be echoed back.
     // The argument AString will automatically receive
     // the value provided as somestring.
     [kbmMW_Method('EchoString')]
     [kbmMW_Rest(
       'method:get, '+
       'path: [ "echostring/{AString}","myechostring/{AString}" ]')]
     function EchoString(
       [kbmMW_Rest('value: "{AString}"')]
         const AString:string):string;
 
     // This method is only available from REST calls, not
     // from smart clients. (kbmMW_Method attribute is missing)
     // It can be called from a browser like this:
     // http://.../someabspath/addnumbers?arg1=10&arg2=20
     // The functions arguments are automatically populated
     // with the arguments.
     [kbmMW_Rest('method:get, path: "/someabspath/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;
 
     // This method gets and owns a TPerson instamce, which it must
     // itself free. The method is not available for REST calls.
     [kbmMW_Method]
     function StorePerson(
       [kbmMW_Arg(mwatOwnedValue)]const APerson:TPerson):integer;
 
     // This method is only available from smart clients.
     // It receives a TPerson instamce which is owned by the
     // caller. When called from a smart client, the kbmMW framework
     // is the owner and will automatically free it.
     [kbmMW_Method]
     function StorePerson2(const APerson:TPerson):integer;
 
     // This method is only available from smart clients.
     // It receives a IPerson2 interface.
     [kbmMW_Method]
     function StorePerson3(const APerson:IPerson2):integer;
 
     // This method can be called both from REST and
     // from smart clients.
     // The method returns a TPerson instamce which is
     // automatically freed by the kbmMW framework after marshalling.
     // (Its default to free a returned object)
     [kbmMW_Rest('method:get, path: "getperson/{id}", freeResult:true')]
     [kbmMW_Method(true)]
     function GetPerson([kbmMW_Rest('value:{id}')]id:string):TPerson;
  end;
 
// Functions published by the service.
//------------------------------------
 
function TkbmMWCustomService2.HelloWorld:string;
begin
     Result:='Hello world';
end;
 
function TkbmMWCustomService2.EchoString(const AString:string):string;
begin
     Result:=AString;
end;
 
function TkbmMWCustomService2.ReverseString(
   const AString:string;
   const AClientIdentity:TkbmMWClientIdentity):string;
begin
     Result:=StrUtils.ReverseString(AString);
end;
 
function TkbmMWCustomService2.AddNumbers(
   const AValue1,AValue2:integer;
   const ARemoteLocation:string):integer;
begin
     Result:=AValue1+AValue2;
 
     // In ARemoveLocation the reported remote location
     // for the client is found.
end;
 
function TkbmMWCustomService2.StorePerson(
   const APerson:TPerson):integer;
begin
     // The TPerson instance is owned by this function,
     // and must be manually freed.
     APerson.Free;
     Result:=0;
end;
 
function TkbmMWCustomService2.StorePerson2(
   const APerson:TPerson):integer;
begin
     // The TPerson instance is automatically freed
     // upon exit of this function.
     Result:=0;
end;
 
function TkbmMWCustomService2.StorePerson3(
   const APerson:IPerson2):integer;
begin
     // The lifetime of IPerson2 depends on reference counting.
     Result:=0;
end;
 
function TkbmMWCustomService2.GetPerson([id:string):TPerson;
begin
     Result:=TPerson.Create;
     Result.Name:='Sofie Mogensen';
     Result.Age:=87;
end;
 
initialization
  TkbmMWRTTI.EnableRTTI(TkbmMWCustomService2);
 
从浏览器中发启REST调用:
 
A) http://localhost/myserver/getperson/1
 
   Results in this response:
 
   {"person":{"Name":"Sofie Mogensen","Address":"","Age":87}}
 
B) http://localhost/someabspath/addnumbers?arg1=10&arg2=20
 
   Results in this response
 
   30
 
Its dead easy to create the server. Just have a central form/datamodule
with the kbmMWServer instance, a transport with the new REST
streamformat setup, and configured to listen on port 80, and add all the
smart service units you want (like the above).
Do:
 
kbmMWServer1.AutoRegisterServices;
kbmMWServer1.Active:=true;
 
and you are running your REST and smart client server!
 
As usual all this can be combined with traditional services and clients.
While REST calls results in objects automatically being streamed and
unstreamed as JSON, smart clients will automatically stream and unstream
object and interface instances in messagepack format, which is now also
supported.
 
来源:官方新闻组
 
http://blog.sina.com.cn/s/blog_44fa172f0102whuv.html

Easy smart REST with kbmMW的更多相关文章

  1. JIRA 知多少:聊一聊 Android Studio 、工作流相关设置

    Android Studio 相关 配置 JIRA 服务器 如果细心的话会发现有一个选项卡:Commit Message.这一段代码是不是有点熟悉呢?你没有猜错,这段代码就是 commit 模板,当你 ...

  2. REST easy with kbmMW #15 – Handling HTTP POST

    我被问到有关如何通过基于kbmMW智能服务(Smart Service)的REST处理POST的问题. 这篇博客文章解释了典型的POST各种形式的访问,以及如何在kbmMW中处理它们. POST变种W ...

  3. REST easy with kbmMW #21 – Delphi client stubs

    在之前的博文中,我提到新的存根生成器框架具有生成Delphi客户端存根所需的功能,使得开发Delphi智能客户端非常容易,完全支持编译时的类型检查和IDE类/属性帮助. 我没想到会把它包含在即将发布的 ...

  4. REST easy with kbmMW #20 – OpenAPI and Swagger UI

    即将推出的kbmMW更新不仅是一些bug修正,同时将包含一个新的主要功能:客户端存根生成器框架. 那什么是客户端存根生成器框架呢? 他是一个基于kbmMW smart services,可以生成由各种 ...

  5. Delphi revelations #1 – kbmMW Smart client on NextGen (Android) – Scope problems

    Delphi 启示 #1 – kbmMW Smart client on NextGen (Android) – 作用域问题 以更高级的方式使用kbmMW smart client,在Android设 ...

  6. 使用delphi-cross-socket 开发kbmmw smart http service

    前几天我说了使用delphi-cross-socket 扩展kbmmw 的跨平台支持,今天我说一下使用 kbmMWCrossScoketHttpServerTransport 在linux 下支持 k ...

  7. 使用kbmmw smart service 属性时的一个注意事项

    kbmmw 5.0 以后支持smart service, 这个用起来非常方便,kbmmw 通过 定制属性来简化编程,可以参考我以前的文章.但是这个意味着使用单元引用一定要小心, 否则出了问题,都不知道 ...

  8. 一窥kbmmw中的 smart service

    在kbmmw 的新版中(还没有发布),将会有一个叫做smart service 的服务.这种服务的属性基于服务器端,并且可以自动注册服务名,下面就是一个简单例子代码.这个服务里面有有三个发布的函数:e ...

  9. REST easy with kbmMW #3 – SSL

    我在前两篇文章中展示了“REST easy with kbmMW”文章,如何使用kbmMW制作REST服务器,以及如何使用该REST服务器轻松地从数据库返回和存储数据,所有这些都在不到30行的真实数据 ...

随机推荐

  1. 关于java读取文件IO流学习总结(一)

    IO流的分类: 1.根据流的数据对象来分: 高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Outpu ...

  2. 淘金(bzoj 3131)

    Description 小Z在玩一个叫做<淘金者>的游戏.游戏的世界是一个二维坐标.X轴.Y轴坐标范围均为1..N.初始的时候,所有的整数坐标点上均有一块金子,共N*N块.    一阵风吹 ...

  3. Iptables入门教程

    转自:http://drops.wooyun.org/tips/1424 linux的包过滤功能,即linux防火墙,它由netfilter 和 iptables 两个组件组成. netfilter ...

  4. ElasticSearch集群状态查看命令大全

    Elasticsearch中信息很多,同时ES也有很多信息查看命令,可以帮助开发者快速查询Elasticsearch的相关信息. _cat $ curl localhost:9200/_cat =^. ...

  5. Codevs 2989 寻找somebody

    时间限制: 2 s 空间限制: 8000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个n*m的方阵中 寻找somebody的位置 有可能k不存在输出“bianta ...

  6. MySQL导出数据库、数据库表结构、存储过程及函数【用】

    一.导出数据库 我的mysql安装目录是D:\Program Files\MySQL\MySQL Server 5.5\bin\,导出文件预计放在D:\sql\ 在mysql的安装目录执行命令: my ...

  7. HDU 4770 Lights Against Dudely 暴力枚举+dfs

    又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...

  8. 使用MYSQL命令直接导入导出SQL文件(转)

    参考:http://blog.csdn.net/jiary5201314/article/details/52026816 1.MYSQL中将数据库导出成SQL文件 其实很简单的,就是一条语句就可以了 ...

  9. 关于MySQL的事务处理及隔离级别

    原文地址 :http://blog.sina.com.cn/s/blog_4c197d420101awhc.html 事务是DBMS得执行单位.它由有限得数据库操作序列组成得.但不是任意得数据库操作序 ...

  10. call 和 apply 方法区别

    在js中call和apply它们的作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数方式有所区别,下面我来给大家介绍一下call和apply用法. 在web前端开发过程中,我们经常需要改变th ...