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

运行结果

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

kbmmw 的HTTPSmartService入门的更多相关文章

  1. kbmmw 的HTTPSmartService 上传文件到服务器端

    前面我写过了 HTTPSmartService 使用介绍,可以参见以前的文章. 前一向有同学问如何在http 页面表单上上传文件.一直没有时间回答,自己简单做了例子, 发现无法实现功能,今天花了一天时 ...

  2. kbmmw 的HTTPSmartService中的跨域访问

    有同学在使用kbmmw 与extjs 结合的时候,涉及到了跨域访问,这个在 kbmmw 里面已经完全解决. extjs 在访问跨域的时候,首先会使用OPIONS  调用,服务端要根据浏览器请求的 he ...

  3. kbmmw 中虚拟文件操作入门

    kbmmw 中一直有一个功能,但是基本上都没有提过,但是在实际应用中,却非常有用,这个功能就是 虚拟文件包功能,他可以把一大堆文件保存到一个文件里面,方便后台管理. kbmmw 的虚拟文件在单元kbm ...

  4. kbmmw 中XML 操作入门

    delphi 很早以前就自带了xml 的操作,最新版里面有三种XML 解释器,一种是MSXML,看名字就知道 这个是微软自带的,这个据delphi 官方称是速度是最快的,但是只能在windows 上使 ...

  5. kbmmw 中JSON 操作入门

    现在各种系统中JSON 用的越来越多.delphi 也自身支持JSON 处理. 今天简要说一下kbmmw 内部如何使用和操作JSON. kbmmw 中json的操作是以TkbmMWJSONStream ...

  6. kbmmw 中XML 操作入门(跨平台,而且可以与JSON,YAML,BSON 直接互相转换)

    delphi 很早以前就自带了xml 的操作,最新版里面有三种XML 解释器,一种是MSXML,看名字就知道 这个是微软自带的,这个据delphi 官方称是速度是最快的,但是只能在windows 上使 ...

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

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

  8. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  9. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

随机推荐

  1. STL::set/multiset

    set:  Sets are containers that store unique elements following a specific order.集合里面的元素不能修改,只能访问,插入或 ...

  2. cf-Global Round2-C. Ramesses and Corner Inversion(思维)

    题目链接:http://codeforces.com/contest/1119/problem/C 题意:给两个同型的由0.1组成的矩阵A.B,问A能否经过指定的操作变成B,指定操作为在矩阵A中选定一 ...

  3. poj 1789 prime

    链接:Truck History - POJ 1789 - Virtual Judge  https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...

  4. 高级测试岗位面试题---MARK

    直接手写一个python类 直接手写一个构造函数 紧接着上面的代码,直接手写,补充完整代码,要求对列表中的人进行排序,并筛选出分数大于80的人的名单,组成一个新的列表显示出来. class Perso ...

  5. stark组件之pop操作【模仿Django的admin】

    一.先看下什么django的admin的pop到底是个什么东西 其实就是这么一个东西, a.在添加页面,在一对多和多对多的项后加了一个+号 b.点击这个加号,会弹出对应的添加 页面,在新的添加 c.添 ...

  6. Bootstrap(1) 概述与环境搭建

    视频教程:http://study.163.com/course/courseMain.htm?courseId=1017002 源码和笔记:http://pan.baidu.com/s/1c06Ri ...

  7. WEB框架之Ajax

    一 Ajax简介 1 Ajax的介绍 AJAX翻译成中文就是"异步Javascript和XML".即使用JavaScript语言与服务器进行异步交互,传输的数据为XML(当然,传输 ...

  8. pthreads v3下一些坑和需要注意的地方

    一.子线程无法访问父线程的全局变量,但父线程可以访问子线程的变量 <?php class Task extends Thread { public $data; public function ...

  9. php 多进程 父进程的阻塞与非阻塞

    php中进程的阻塞,主要是父进程等待子进程退出. 1.php代码如下: <?php //定义进程数量 define('FORK_NUMS', 5); //用于保存进程pid $pids = ar ...

  10. u-boot之内核是怎么启动的

    在u-boot之start_armboot函数分析已经分析过了整个程序框架,但只是说了下什么时候运行内核,并没有具体说明是怎么执行内核的.内核启动分以下几个步骤说明: 1.启动参数bootcmd=na ...