13.元数据
本节详细介绍了基于Spring Data Rest为基础的应用程序提供的各种形式的元数据。
13.1. 应用层简介语义(ALPS)
ALPS 和HTMl有着相同复杂性的微格式,一个ALPS文件可以作为一个剖面解释与应用无关的媒体类型文件的应用程序的语义(如HTML,hal,Collection+JSON,Siren,等)。 这增加了跨媒体类型的配置文件的可重用性。
— M. Admundsen / L. Richardson / M. Foster http://tools.ietf.org/html/draft-amundsen-richardson-foster-alps-00
Spring Data REST provides an ALPS 为每一个导出的存储库提供一个ALPS文档,它包含了两个关于平安转换的信息以及各库的属性。
Spring Data Rest的应用程序的根是一个配置文件profile 链接,假设你有一个应用程序与两个人相关的地址,根文件将看起来像这样:
{"_links":{"persons":{"href":"http://localhost:8080/persons"},"addresses":{"href":"http://localhost:8080/addresses"},"profile":{"href":"http://localhost:8080/profile"}}}
一个 profile 链接,比如 RFC 6906, is a place to include application level details. The ALPS draft spec ALPS草案风格是为了定义一个特定的配置文件格式, 我们将进一步探讨在这一部分
,如果你浏览到本地主机的 profile 链接 localhost:8080/profile
,你将会看到这些:
{"_links":{"self":{"href":"http://localhost:8080/profile"},"persons":{"href":"http://localhost:8080/profile/persons"},"addresses":{"href":"http://localhost:8080/profile/addresses"}}}
在根级别, profile 是一个单一的链接,因此无法处理多个应用程序配置文件。这就是为什么你必须导航到 /profile 为每个资源的元数据找到一个链接. |
|
---|---|
让我们导航到 /profile/persons
看看个人资源的配置文件数据 Person
资源.
{"version":"1.0","descriptors":[{"id":"person-representation",(1)"descriptors":[{"name":"firstName","type":"SEMANTIC"},{"name":"lastName","type":"SEMANTIC"},{"name":"id","type":"SEMANTIC"},{"name":"address","type":"SAFE","rt":"http://localhost:8080/profile/addresses#address"}]},{"id":"create-persons",(2)"name":"persons",(3)"type":"UNSAFE",(4)"rt":"#person-representation"(5)},{"id":"get-persons","name":"persons","type":"SAFE","rt":"#person-representation"},{"id":"delete-person","name":"person","type":"IDEMPOTENT","rt":"#person-representation"},{"id":"patch-person","name":"person","type":"UNSAFE","rt":"#person-representation"},{"id":"update-person","name":"person","type":"IDEMPOTENT","rt":"#person-representation"},{"id":"get-person","name":"person","type":"SAFE","rt":"#person-representation"}]}
1 | 顶部是一个详细的个人资源属性的列表 Person 确定为 #person-representation . 它列出属性的名称。 |
---|---|
2 | 在资源表示后,所有支持的操作。折是如何创造一个新的 Person . |
3 | 这个名字是 persons, 这表明一个帖子应该适用于整个集合,而不是一个单一的 person. |
4 | 这种type 是UNSAFE 因为这种操作可以改变系统的状态。 |
这个JSON文档有一个 application/alps+json . 这是不同于以往的JSON文档,其中有一个 application/hal+json .这些格式是不同的,受不同的规格。 |
|
---|---|
你将找到一个 "profile" 在收集显示链接时,你看到的是一个收集资源。_links when you are looking at a collection resource.
{"_links":{"self":{"href":"http://localhost:8080/persons"(1)},... other links ..."profile":{"href":"http://localhost:8080/profile/persons"(2)}},...}
1 | 这个HAl文件代表 Person 收藏. |
---|---|
2 | 它有一个 profile 链接到同一个URI元数据。 |
The profile link,如果你使用一个接受应用程序/ALPS+ JSON的标头,ALPS默认将再次服务配置文件链接。 Accept header of application/alps+json.
13.1.1. 超媒体的控制类型
ALPS显示每个超媒体控件的类型,他们包括:
Table 4. ALPS 类型
类型 | 描述 |
---|---|
语义 | 一个元 (e.g. HTML.SPAN, HTML.INPUT, 等.). |
安全 | 触发一个安全的超媒体控制, 幂等状态的改变 (e.g. GET or HEAD). |
幂等 | 触发不安全的超媒体控制, 幂等态的转变 (e.g. PUT or DELETE). |
不安全 | 触发不安全的超媒体控制, 非幂等态转变 (e.g. POST). |
在上面的表示部分中,应用程序中的数据位标记为 SEMANTIC,address是一个链接,涉及到一个安全的检索GET, 它是显著安全的标记 SAFE,超媒体操作本身映射到所显示的表的类型。
13.1.2. ALPS with Projections
如果您定义任何预测,他们也将在ALPS元数据中列出。假设我们也定义 inlineAddress 和 noAddresses, 会出现相关的内部操作。,i.e. GET 对于整个集合以及 GET,即得到整个收集也得到一个单一的资源. 下面的显示版本的备用版本 get-persons 部分:
...{"id":"get-persons","name":"persons","type":"SAFE","rt":"#person-representation","descriptors":[{(1)"name":"projection","doc":{"value":"The projection that shall be applied when rendering the response. Acceptable values available in nested descriptors.","format":"TEXT"},"type":"SEMANTIC","descriptors":[{"name":"inlineAddress",(2)"type":"SEMANTIC","descriptors":[{"name":"address","type":"SEMANTIC"},{"name":"firstName","type":"SEMANTIC"},{"name":"lastName","type":"SEMANTIC"}]},{"name":"noAddresses",(3)"type":"SEMANTIC","descriptors":[{"name":"firstName","type":"SEMANTIC"},{"name":"lastName","type":"SEMANTIC"}]}]}]}...
1 | 一个新的属性, descriptors,描述符包含了一个数组的投影。projection. |
---|---|
2 | 里面的 projection.descriptors我们能看见inLineAddress 这将使 address, firstName,和 lastName. 呈现在投影结果在内联数据字段的关系。 |
3 | 在这里发现 noAddresses,它提供了一个子集 firstName和 lastName. |
有了这些信息,客户应该能够推断不仅平安过渡可用,而且,在某种程度上,数据元素需要互动。
13.1.3. 添加自定义的细节到您的ALPS的描述
有了这些信息,客户能够推断不仅平安过渡可用,而且,在某种程度上,数据元素需要互动。
rest-messages.properties
就像这样:
rest.description.person=A collection of people
rest.description.person.id=primary key used internally to store a person (notforRESTful usage)
rest.description.person.firstName=Person's first name
rest.description.person.lastName=Person's last name
rest.description.person.address=Person's address
正如你所看到的,这定义了一个用于显示 Person
的详细信息. 他们改变了person-representation 形式如下:
...{"id":"person-representation","doc":{"value":"A collection of people",(1)"format":"TEXT"},"descriptors":[{"name":"firstName","doc":{"value":"Person's first name",(2)"format":"TEXT"},"type":"SEMANTIC"},{"name":"lastName","doc":{"value":"Person's last name",(3)"format":"TEXT"},"type":"SEMANTIC"},{"name":"id","doc":{"value":"primary key used internally to store a person (not for RESTful usage)",(4)"format":"TEXT"},"type":"SEMANTIC"},{"name":"address","doc":{"value":"Person's address",(5)"format":"TEXT"},"type":"SAFE","rt":"http://localhost:8080/profile/addresses#address"}]}...
通过提供这些属性设置, 每个字段都有一个额外的doc 属性.
1 | rest.description.person |
映射到整个表示的值。 |
---|---|---|
2 | rest.description.person.firstName 地图 firstName 属性的值. |
|
3 | The value of rest.description.person.lastName 地图 lastName 属性的值. |
|
4 | The value of rest.description.person.id 地图 id 属性的值,一次不正常显示 |
|
5 | The value of rest.description.person.address 地图 address 属性的值. |
Spring MVC (这是一个 Spring Data REST应用程序,其余数据应用的本质)支持的地方,意味着你可以把多个属性文件与不同的消息整合到一起。 | |
---|---|
13.2. JSON图式
JSON Schema是 Spring Data REST的另一种形式的元数据支持. 他们的网站, JSON 图式 具有以下优点:
描述您现有的数据格式
人类和机器清晰可读的文件
完整的结构验证,有助于自动化测试和验证客户端提交的数据
如前面所示 previous section, 如果你的资源有链接到其他资源,会有更多的细节。你也将找到一个"profile" 链接.
{"_links":{"self":{"href":"http://localhost:8080/profile"},"persons":{"href":"http://localhost:8080/profile/persons"},"addresses":{"href":"http://localhost:8080/profile/addresses"}}}
JSON模式是另一种形式的元数据通过Spring Data Rest数据支持 application/schema+json.
在这种情况下,如果你执行 curl -H 'Accept:application/schema+json' [http://localhost:8080/profile/persons](http://localhost:8080/profile/persons)
, 你会看到这样的东西:
{"title":"org.springframework.data.rest.webmvc.jpa.Person",(1)"properties":{(2)"firstName":{"readOnly":false,"type":"string"},"lastName":{"readOnly":false,"type":"string"},"siblings":{"readOnly":false,"type":"string","format":"uri"},"created":{"readOnly":false,"type":"string","format":"date-time"},"father":{"readOnly":false,"type":"string","format":"uri"},"weight":{"readOnly":false,"type":"integer"},"height":{"readOnly":false,"type":"integer"}},"descriptors":{},"type":"object","$schema":"http://json-schema.org/draft-04/schema#"}
1 | 出口的类型 |
---|---|
2 | 上市的属性 |
会有更多的细节,如果你的资源有链接到其他资源.
你将找到一个 "profile" 链接 显示在集合 _links 当你在看一个集合资源时.
{"_links":{"self":{"href":"http://localhost:8080/persons"(1)},... other links ..."profile":{"href":"http://localhost:8080/profile/persons"(2)}},...}
1 | 这个HAl文件代表个人 Person 收藏 |
---|---|
2 | 它有一个。 profile配置文件链接到同一个URI元数据. |
profile 链接,将再次服务 ALPS 在默认情况下. 如果你给它提供一个 Accept header of application/schema+json, 它将渲染 JSON 的图式表征。