kbmmw 的HTTPSmartService入门

简介: 前面介绍过kbmmw 中的smartservice. 这个既可以用于kbmmw 的客户端,也可以使用http 访问。 在新版的kbmmw里面,作者加强了http 的支持,我们可以只使用HTTPSmartService ,这样可以省去很多代码,可以更方便、简单的实现REST 服务。

前面介绍过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;

运行结果

 

 

 

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

目录
相关文章
|
数据采集 TensorFlow 算法框架/工具
【大作业-03】手把手教你用tensorflow2.3训练自己的分类数据集
本教程详细介绍了如何使用TensorFlow 2.3训练自定义图像分类数据集,涵盖数据集收集、整理、划分及模型训练与测试全过程。提供完整代码示例及图形界面应用开发指导,适合初学者快速上手。[教程链接](https://www.bilibili.com/video/BV1rX4y1A7N8/),配套视频更易理解。
341 0
【大作业-03】手把手教你用tensorflow2.3训练自己的分类数据集
|
8月前
|
数据采集 JSON 数据挖掘
Elasticsearch 的DSL查询,聚合查询与多维度数据统计
Elasticsearch的DSL查询与聚合查询提供了强大的数据检索和统计分析能力。通过合理构建DSL查询,用户可以高效地搜索数据,并使用聚合查询对数据进行多维度统计分析。在实际应用中,灵活运用这些工具不仅能提高查询效率,还能为数据分析提供深入洞察。理解并掌握这些技术,将显著提升在大数据场景中的分析和处理能力。
423 20
|
机器学习/深度学习 测试技术 TensorFlow
【大作业-01】花卉识别-基于tensorflow2.3实现
2021年6月18日更新:提供修复后的TensorFlow 2.3物体分类代码,支持自定义数据集训练。包含CSDN教程、B站视频、数据集及代码下载链接。示例项目为花卉识别,涵盖模型训练、测试、保存和使用,附带图形界面操作指南。
226 0
【大作业-01】花卉识别-基于tensorflow2.3实现
Go字节数组与字符串相互转换
Go字节数组与字符串相互转换
258 3
|
NoSQL 安全 Shell
MongoDB 用户管理
10月更文挑战第12天
377 0
|
XML 设计模式 Java
PowerMock:静态方法与私有方法测试
PowerMock是Java单元测试中扩展Mockito的框架,允许模拟静态方法、构造函数、私有方法和final类,以增强测试隔离和覆盖率。主要应用场景包括静态方法模拟、私有方法测试和构造函数/Final类模拟。然而,使用时需注意配置复杂性、避免过度使用、精确控制模拟行为和遵循最佳实践。示例展示了如何模拟静态方法,通过添加PowerMock依赖和使用PowerMockito.mockStatic进行静态方法的模拟和验证。正确使用PowerMock能提升测试质量,但应谨慎以保持代码可读性和测试有效性。
688 5
PowerMock:静态方法与私有方法测试
|
Web App开发 开发者
本地安装谷歌的插件之 CRX格式插件离线安装
本地安装谷歌的插件之 CRX格式插件离线安装
1341 0
|
缓存 Linux API
Linux V4l2视频设备
Linux V4l2视频设备
|
SQL 安全 关系型数据库
【SQL Server】使用 LinkedServer 进行远程链接 MySql 数据库
【SQL Server】使用 LinkedServer 进行远程链接 MySql 数据库
707 0
【SQL Server】使用 LinkedServer 进行远程链接 MySql 数据库