简体   繁体   English

如何为由多列主键组成的json对象构建URL?

[英]how to build a URL for a json object consisting of a multiple column primary key?

Step 1: 第1步:

If i have a json object 如果我有一个json对象

{
    "person" : {
        "id" : 1,
        "lastName" : "Hammer",
        "firstName" : "Mike",
        ...
    },
}

I can address the object by its name and ID 我可以通过对象的名称和ID来寻址

GET http://host/persons/1/

Step 2: 第2步:

Now I have a data model containing a primary key consisting of multiple attributes. 现在,我有一个数据模型,其中包含一个由多个属性组成的主键。

for example primary key is (firstName,lastName). 例如主键是(firstName,lastName)。 There is no single primary key like "id". 没有像“ id”这样的单个主键。

{
   "person" : {
      "lastName" : "Hammer",
      "firstName" : "Mike",
      ...
   },
}

What is the syntax to build a URL for this? 为此构建URL的语法是什么?

GET http://host/persons/???

I believe the rendering of : 我相信渲染:

GET http://host/persons/1/

is based on custom code. 基于自定义代码。

then you could use the following scheme for multi-field keys : 那么您可以对多字段键使用以下方案:

GET http://host/persons/field1/field2

in the given case: 在给定的情况下:

GET http://host/persons/lastName/firstname  

In general URIs as defined by RFC 3986 (see Section 2: Characters) may contain any of the following characters: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=. 通常,由RFC 3986(请参见第2节:字符)定义的URI可以包含以下任何字符:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 -._〜:/?#[] @!$&'()* +,; =。 Any other character needs to be encoded with the percent-encoding (%hh) . 其他任何字符都需要使用percent-encoding(%hh)进行编码。

Your could read up on good practices for REST ful URIs on the web such as : http://blog.2partsmagic.com/restful-uri-design/ 您可以阅读有关Web上REST ful URI的良好做法,例如: http : //blog.2partsmagic.com/restful-uri-design/

If you would like to include meta-data such as field-names in the url , you would have to put it in as key=value format for each field in the primary key of a multi-part key separated by a semicolon. 如果您想在url中包括诸如字段名之类的元数据,则必须将其以key = value格式放入每个由分号分隔的多部分键的主键中的每个字段中。

ex: GET http://host/persons/lastName=Doe;firstname=John/address/home 例如: GET http://host/persons/lastName=Doe;firstname=John/address/home

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

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