我在 Delphi 7 的应用程序中嵌入了一个(非常简单)自制的 REST 服务器(带有 ICS + 一些东西),它可以工作,但不容易维护和扩展。 现在我使用 Delphi XE2 Pro(没有 DataSnap),我会改用更标准的解决方案,但又很简单。
有一个很好的简单方法吗?
最佳答案
Habari Web Components框架是一个简单的(商业)HTTP 服务器框架,适用于 Delphi 2009 及更高版本。随着TdjRestfulComponent它还包括 REST 扩展。 (我是这些库的开发者)
TdjRestfulComponent 配置可以以类似属性/注释的样式或更传统的过程样式方式完成。
所有 HTTP 方法和内容类型都可以映射到不同的匿名方法,并且仍然共享相同的资源 URI(一个 URI,不同的资源表示 - 取决于请求的内容类型)。例如,要以 HTML、XML 或 JSON 表示资源 /myresource
,可以这样配置:
// respond to HTML browser GET request
&Path('myresource');
&Produces('text/html');
GET(procedure(Request: TRequest; Response: TResponse)
begin
Response.ContentText := '<html>Hello world!</html>';
end);
// respond to XML client
&Path('myresource');
&Produces('application/xml');
GET(procedure(Request: TRequest; Response: TResponse)
begin
Response.ContentText := '<xml>Hello world!</xml>';
end);
// respond to JSON client
&Path('myresource');
&Produces('application/json');
GET(procedure(Request: TRequest; Response: TResponse)
begin
Response.ContentText := '{"msg":"Hello world!"}';
end);
该组件还支持路径参数:
&Path('orders/{orderId}/lines/{lineNo');
将解析如下 URL
http://mydomain.local:8080/context/orders/65432/lines/1
进入其他查询参数(orderId=65431
和 lineNo=1
)
关于delphi - Delphi XE2 pro 中的 REST 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10535257/