简体   繁体   English

Delphi XE2:如何定义自定义DataSnap REST URI?

[英]Delphi XE2: How to define custom DataSnap REST URI?

I am using Delphi XE2 to write DataSnap REST service. 我正在使用Delphi XE2编写DataSnap REST服务。 I notice that the REST URI in DataSnap must strictly follow this format (refer here ): 我注意到DataSnap中的REST URI必须严格遵循以下格式(请参阅此处 ):

http://my.site.com/datasnap/rest/URIClassName/URIMethodName[/inputParameter]*

A famous example is sample method create by DataSnap server wizard: 一个著名的示例是DataSnap服务器向导创建的示例方法:

http://my.site.com/datasnap/rest/TServerMethods1/ReverseString/ABC

There are 2 common ways to supply parameters in URI: 有两种在URI中提供参数的常用方法:

  1. Path Segment parameter: /TServerMethods1/ReverseString/ABC 路径段参数:/ TServerMethods1 / ReverseString / ABC
  2. Query String parameter: /TServerMethods1/customers?name=bill 查询字符串参数:/ TServerMethods1 / customers?name = bill

The Path Segment parameter URI is definitely supported by DataSnap REST. DataSnap REST绝对支持路径段参数URI。 Is Query string parameters URI support in DataSnap REST too? DataSnap REST也支持查询字符串参数URI吗?

I have the following REST URI example and found it seems impossible to make it work with current DataSnap REST library: 我有以下REST URI示例,发现似乎无法使其与当前的DataSnap REST库一起使用:

  1. /customers/A1234 / customers / A1234

    return customer object of ID A1234 返回ID为A1234的客户对象

  2. /customers/A1234.xml /customers/A1234.xml

    return customer object of ID A1234 in XML format 以XML格式返回ID为A1234的客户对象

  3. /customers/A1234.json /customers/A1234.json

    return customer object of ID A1234 in json format 以json格式返回ID A1234的客户对象

  4. /customers/A1234.html /customers/A1234.html

    return customer object of ID A1234 in html format 以html格式返回ID为A1234的客户对象

  5. /customers?name=Bill / customers?name =比尔

    return a list of customer whose name contain Bill 返回名称包含Bill的客户列表

I don't know how to do it using DataSnap, but there are ways around it. 我不知道如何使用DataSnap做到这一点,但是有很多解决方法。 You can put something called URLRewrite to good use for this as both your friendly URI's and the ones required by DataSnap are easily mappable. 您可以为此使用URLRewrite之类的东西,因为您的友好URI和Da​​taSnap所需的URI都可以轻松映射。

For IIS you can use (enable) the URLRewrite module which is standard in IIS 7. More information can be found on the official site: http://www.iis.net/download/urlrewrite . 对于IIS,您可以使用(启用)IIS 7中的标准URLRewrite模块。有关更多信息,请参见官方网站: http : //www.iis.net/download/urlrewrite

Be sure to create rules for inbound and outbound URI's so that the "internal" (Datasnap) URI's do not get out into the wild. 确保为入站 出站 URI创建规则,以免“内部”(Datasnap)URI泛滥成灾。

If you are running the site on Apache, similar functionality is available, and I thin you need to amend the .htaccess file, but I have no experience with Apache so I could be wrong. 如果您在Apache上运行该站点,则可以使用类似的功能,并且我认为您需要修改.htaccess文件,但是我没有Apache的经验,所以我可能是错的。

A bit late to the party, but yes you can use query parameters. 聚会晚了一点,但是可以使用查询参数。

You have to use GetInvocationMetadata.QueryParams 您必须使用GetInvocationMetadata.QueryParams

see the example below. 请参阅下面的示例。

uses DBXPlatform;

function TServerMethods1.EchoString(Value: string): string;
var
  metaData: TDSInvocationMetadata;
  i: integer;
begin
  metaData := GetInvocationMetadata;
  for i := 0 to Pred(metaData.QueryParams.Count) do
  begin
    Result := Result + '<param>' + metaData.QueryParams[i] + '</param>';
  end;
  metaData.ResponseContent := '<xml>' + Result + '</xml>';
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM