zoukankan      html  css  js  c++  java
  • kbmmw 的HTTPSmartService入门

    前面介绍过kbmmw 中的smartservice. 这个既可以用于kbmmw 的客户端,也可以使用http 访问。

    在新版的kbmmw里面,作者加强了http 的支持,我们可以只使用HTTPSmartService

    ,这样可以省去很多代码,可以更方便、简单的实现REST 服务。

    首先先建一个工程文件,放置对应的控件。

     

    新建一个kbmmw service

    选HTTP smart service ,(这个向导界面太丑了,希望作者以后能请个美工处理一下)。

    剩下的一路点过去,到最后生成代码。

    回到主窗口,输入以下对应的代码

    procedure TForm1.Button1Click(Sender: TObject);
    begin
     kbmmwserver1.Active:=True;
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
    kbmmwserver1.AutoRegisterServices;
    end;

    再看看生成的代码

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

    由于我们是要做rest,因此,修改一下helloworld 的代码,使其更符合rest 的格式

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

    ok, 编译运行,使用浏览器访问

     没有任何问题,非常简单方便。

     我们可以直接增加新的函数。

         [kbmMW_Rest('method:get, path:version')]
         [kbmMW_Method]
         function version:string;
    
      end;
    
    implementation
    
    uses kbmMWExceptions;
    
    {$R *.dfm}
    
    
    // Service definitions.
    //---------------------
    
    function TkbmMWCustomHTTPSmartService1.version: string;
    begin
          Result:='{"result":"'+self.Server.Version+'"}';
    end;

    运行显示

    处理参数

    如果只有一个参数,可以直接使用 http://127.0.0.1/xalionrest/echostring/{参数}

    [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;
    function TkbmMWCustomHTTPSmartService1.EchoString(
      const AString: string): string;
    begin
         result:='{"result":"你好!'+astring+'"}';;
    end;

    如果是多个参数,可以使用http://127.0.0.1/xalionrest/cal/numbers?arg1=10&arg2=20

    [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):string;
    function TkbmMWCustomHTTPSmartService1.AddNumbers(const AValue1,
      AValue2: integer; const ARemoteLocation: string):string;
    begin
          Result:='{"result":"'+(AValue1+AValue2).ToString+'"}';;
    end;

    运行结果

    下面处理一下post 的函数,首先做一个含有form 的html 文件

    <table width="770" border="0" align="center" cellpadding="0" cellspacing="0" class="unnamed2">
      <tr>
           
        <td width="848" align="center"><span class="style1">新生录取查询</span><br></td>
    
      </tr>
      <form name="form1" method="post" action="/xalionrest/postdata">
      <tr>
        <td align="center">
                 <span class="style2">姓名:</span>          <input name="xsxm" type="text" id="xsxm">
               <span class="style2">身份证号:</span>          <input name="sfzh" type="text" id="sfzh">
         </td>
      </tr>
      
      <tr>
    
        <td align="center">
          <br>
          <input type="submit" name="Submit" value="提交" onClick="return B1_onclick()">        
          <input type="reset" name="Submit" value="重置">
        </td>
      </tr>
     </form> 
    </table>

    对应的函数为

     [kbmMW_Rest('method:post, path:postdata')]
         [kbmMW_Method]
         function postdata:string;
    function TkbmMWCustomHTTPSmartService1.postdata: string;
    var
      vl:TkbmMWHTTPCustomValues;
      s,xsxm,sfzh:string;
    
      p:Tbytes;
     begin
           vl:=TkbmMWHTTPQueryValues.Create;
                   try
    
                    p:= RequestStream.SaveToBytes;
    
                    vl.AsString:= Tencoding.ASCII.GetString(p);
    
                      xsxm:= vl.ValueByName['xsxm'];
                      sfzh:=vl.ValueByName['sfzh'];
    
                         // 这里就可以向数据库里面操作了
    
                       result:='姓名:'+xsxm+'      身份证号'+sfzh;
                       finally
    
                        vl.Free ;
                       end;
    
             SetResponseMimeType('text/html');
    
    
    end;

    运行结果

     基本上就是这样。

    kbmmw 5.04.40 新增加了post 内容文本自动识别功能,上面的函数可以变得更简单。

     [kbmMW_Rest('method:post, path:poststring')]
         [kbmMW_Method]
         function poststring([kbmMW_Rest('value: "body", required: true')] const body:string):string;
    function TkbmMWCustomHTTPSmartService1.poststring(const body: string): string;
    var
      vl:TkbmMWHTTPCustomValues;
      s,xsxm,sfzh:string;
    begin
         vl:=TkbmMWHTTPQueryValues.Create;
                   try
    
    
                    vl.AsString:= body;
                    xsxm:= vl.ValueByName['xsxm'];
                    sfzh:=vl.ValueByName['sfzh'];
    
                         // 这里就可以向数据库里面写了
    
    
                        result:='姓名:'+xsxm+'      身份证号'+sfzh;
                       finally
    
                        vl.Free ;
                       end;
    
             SetResponseMimeType('text/html');
    
    end;

    运行结果

    后面我们再介绍数据库的操作。

  • 相关阅读:
    oracle数据导入/导出
    table中某一个tr边框样式设置
    错误Batch update returned unexpected row count from update [0]; actual row count: 0;
    错误信息:attempt to create saveOrUpdate event with null entity
    hibernate 异常:Unexpected Exception caught setting
    SVN Cleanup failed的解决办法
    slf4j-simple的配置
    Jquery Validate根据其他元素的事件来触发单个元素的异步校验
    Tomcat以指定JDK运行
    Spring MVC 接收Json格式参数
  • 原文地址:https://www.cnblogs.com/xalion/p/7895179.html
Copyright © 2011-2022 走看看