• Easy smart REST with kbmMW


    使用新版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
  • 相关阅读:
    零售行业解决方案一
    N-Tier Entity Framework开源项目介绍
    软件代码生成之Codesmith模板.netTiers
    ActiveMQ消息队列介绍
    EntityFramework动态多条件查询与Lambda表达式树
    GitLab版本管理
    REST服务介绍
    2014年物联网Internet of Things应用简介
    wordpress如何屏蔽wp-json(禁用REST API)
    bootstrap tab切换如何让鼠标移动自动切换内容
  • 原文地址:https://www.cnblogs.com/findumars/p/6436008.html
Copyright © 2020-2023  润新知