博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kbmmw 的HTTPSmartService入门
阅读量:7113 次
发布时间:2019-06-28

本文共 5122 字,大约阅读时间需要 17 分钟。

前面介绍过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);beginkbmmwserver1.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;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;implementationuses 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 文件

新生录取查询
姓名: 身份证号:
        

 

 

对应的函数为

 

[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;

运行结果

 

 

 

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

转载地址:http://yuqhl.baihongyu.com/

你可能感兴趣的文章
简单工厂模式 & 工厂方法模式 & 抽象工厂模式
查看>>
如何关闭visual studio2005实时调试器
查看>>
DotNetBar for Windows Forms 12.2.0.7_冰河之刃重打包版原创发布-带官方示例程序版
查看>>
oracle存储过程
查看>>
HTTP协议详解
查看>>
svn的搭建与使用
查看>>
大型网站技术架构(五)网站高可用架构
查看>>
RabbitMQ学习总结(7)——Spring整合RabbitMQ实例
查看>>
大表改造成分区表
查看>>
Maven学习总结(一)——Maven入门
查看>>
[9-13]Shell系列8——数组
查看>>
Java核心技术之10诀窍 广州疯狂JAVA培训
查看>>
web.xml配置详解
查看>>
Git Tool Part 1
查看>>
Dubbo学习总结(4)——Dubbo基于Zookeeper实现分布式实例
查看>>
程序员会被淘汰吗?
查看>>
【小项目 日程表程序】最近frank我想到一个好项目(好吧,我同意不是我想的)...
查看>>
mysql字符集 乱码问题
查看>>
RabbitMQ学习总结(2)——安装、配置与监控
查看>>
JavaScript学习总结(2)——JavaScript数据类型判断
查看>>