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

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

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

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

新建一个kbmmw service

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

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

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

procedure TForm1.Button1Click(Sender: TObject);
beginkbmmwserver1.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;implementationuses kbmMWExceptions;{$R *.dfm}// Service definitions.
//---------------------function TkbmMWCustomHTTPSmartService1.HelloWorld:string;
beginResult:='Hello world';
end;initializationTkbmMWRTTI.EnableRTTI(TkbmMWCustomHTTPSmartService1);
end.

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

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

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

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

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

     [kbmMW_Rest('method:get, path:version')][kbmMW_Method]function version:string;end;implementationuses kbmMWExceptions;{$R *.dfm}// Service definitions.
//---------------------function TkbmMWCustomHTTPSmartService1.version: string;
beginResult:='{"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;
beginresult:='{"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;
beginResult:='{"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;
varvl:TkbmMWHTTPCustomValues;s,xsxm,sfzh:string;p:Tbytes;beginvl:=TkbmMWHTTPQueryValues.Create;tryp:= RequestStream.SaveToBytes;vl.AsString:= Tencoding.ASCII.GetString(p);xsxm:= vl.ValueByName['xsxm'];sfzh:=vl.ValueByName['sfzh'];// 这里就可以向数据库里面操作了
result:='姓名:'+xsxm+'      身份证号'+sfzh;finallyvl.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;
varvl:TkbmMWHTTPCustomValues;s,xsxm,sfzh:string;
beginvl:=TkbmMWHTTPQueryValues.Create;tryvl.AsString:= body;xsxm:= vl.ValueByName['xsxm'];sfzh:=vl.ValueByName['sfzh'];// 这里就可以向数据库里面写了
result:='姓名:'+xsxm+'      身份证号'+sfzh;finallyvl.Free ;end;SetResponseMimeType('text/html');end;

运行结果

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

转载于:https://www.cnblogs.com/xalion/p/7895179.html

kbmmw 的HTTPSmartService入门相关推荐

  1. 无法打开此程序因为计算机丢失,解决X-Scan安装后“无法启动此程序,因为计算机丢失NPPTools.dll”...

    配置CENTOS YUM更新源 众所周知,Centos 有个很方便的软件安装工具  yum,但是默认安装完centos,系统里使用的是国外的centos更新源,这就造成了我们使用默认更新源安装或者更新 ...

  2. 用Construct 2制作入门小游戏~

    今天在软导课上了解到了Construct 2这个神器,本零基础菜鸟决定尝试做一个简单的小游戏(实际上是入门的教程啊= = 首先呢,肯定是到官网下载软件啊,点击我下载~ 等安装完毕后我便按照新手教程开始 ...

  3. Docker入门六部曲——Swarm

    原文链接:http://www.dubby.cn/detail.html?id=8738 准备工作 安装Docker(版本最低1.13). 安装好Docker Compose,上一篇文章介绍过的. 安 ...

  4. Docker入门六部曲——Stack

    原文链接:http://www.dubby.cn/detail.html?id=8739 准备知识 安装Docker(版本最低1.13). 阅读完Docker入门六部曲--Swarm,并且完成其中介绍 ...

  5. Docker入门六部曲——服务

    原文链接:http://www.dubby.cn/detail.html?id=8735 准备 已经安装好Docker 1.13或者以上的版本. 安装好Docker Compose.如果你是用的是Do ...

  6. 【springboot】入门

    简介: springBoot是spring团队为了整合spring全家桶中的系列框架做研究出来的一个轻量级框架.随着spring4.0推出而推出,springBoot可以説是J2SEE的一站式解决方案 ...

  7. SpringBoot (一) :入门篇 Hello World

    什么是SpringBoot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不 ...

  8. 入门指南目录页 -PaddlePaddle 飞桨 入门指南 FAQ合集-深度学习问题

    入门指南目录页 -PaddlePaddle 飞桨 入门指南 FAQ合集 GT_Zhang关注 0.1012019.08.01 18:43:34字数 1,874阅读 795 Hi,欢迎各位来自Paddl ...

  9. 5 分钟入门 Google 最强NLP模型:BERT

    BERT (Bidirectional Encoder Representations from Transformers) 10月11日,Google AI Language 发布了论文 BERT: ...

最新文章

  1. 【学习总结】GirlsInAI ML-diary day-3-数据类型
  2. 白话Elasticsearch47-深入聚合数据分析之Cardinality Aggs-cardinality算法之优化内存开销以及HLL算法
  3. mysql实现类似oracle的序列,mysql 创建[序列],功能类似于oracle的序列
  4. 关于SQL优化这些你了解吗?
  5. oracle之数据处理之约束2
  6. Buttons——CSS按钮样式库
  7. 从串口驱动的移植看linux2.6内核中的驱动模型 platform device platform driver【转】...
  8. C# CollectionBase,ICloneeable
  9. Jenkins学习总结(1)——Jenkins详细安装与构建部署使用教程
  10. [Ext JS 4] 实战之多选下拉单 (带checkbox) 续 - 带ALL 选项
  11. 著名音频库多角度对比(多平台可用)
  12. 面经——2022届CVTE提前批
  13. 图机器学习在度小满风控中的应用
  14. 190407每日一句
  15. 如何制作万有特性曲线图
  16. matlab 计算函数极值,如何用MATLAB求函数的极值点和最大值
  17. 浏览器地址栏中文乱码问题
  18. 捷太格特PC10G与三菱MR-J4的SSCNETⅢ通讯
  19. 潮流计算程序————支路功率计算与输出程序
  20. 《私募股权基金投资基础知识》---第四章

热门文章

  1. 【FAQ】使用 LOAD 載入外部中文字檔 *.TXT, 中文字卻成為亂碼之解決
  2. Delphi 2010 新增功能之: IOUtils 单元(4): TDirectory.GetDirectories
  3. navicat远程连接mysql10060
  4. windows中卸载Jenkins
  5. flask第十八篇——模板【2】
  6. sublime必备插件
  7. iOS:ShareSDk的分享
  8. 参数估计:最大似然、贝叶斯与最大后验
  9. 重新想象 Windows 8 Store Apps (61) - 通信: http, oauth
  10. arm-linux-readelf