可选元素(如 RepositoryRestResource 注解中的 collectionResourceRel)中"rel"的含义是什么?



你能帮我理解RepositoryRestResource注释的collectionResourceRel中提到的"rel"可选元素的含义吗? 我在这里浏览了Java Docs。

以下是文档中所写的内容。

collectionResourceRel 生成指向的链接时要使用的 rel 值 集合资源。

基本上rel@RestResource注释的属性。它的意思是"关系">

例如,订单可能具有将订单与其客户联系起来的"rel" : "customer"关系。

从 https://docs.spring.io/spring-data/rest/docs/current/reference/html/:

例如,在默认配置中,如果向http://localhost:8080/persons/search发出请求以了解公开了哪些查询方法,则会返回类似于以下内容的链接列表:

{
"_links" : {
"findByName" : {
"href" : "http://localhost:8080/persons/search/findByName"
}
}
}

若要更改rel值,请在@RestResource批注上使用rel属性,如以下示例所示:

@RepositoryRestResource(path = "people")
interface PersonRepository extends CrudRepository<Person, Long> {
@RestResource(path = "names", rel = "names")
List<Person> findByName(String name);
}

前面的示例生成以下链接值:

{
"_links" : {
"names" : {
"href" : "http://localhost:8080/persons/search/names"
}
}
}

您可以更改存储库的 rel,如以下示例所示:

@RepositoryRestResource(path = "people", rel = "people")
interface PersonRepository extends CrudRepository<Person, Long> {
@RestResource(path = "names", rel = "names")
List<Person> findByName(String name);
}

更改存储库的 rel 会更改顶级名称,如以下示例输出所示:

{
"_links" : {
"people" : {  // rel = "people"
"href" : "http://localhost:8080/people"
},
…
}
}

rel = "people"将该链接的名称更改为people

问:您是否有示例显示"订单可能具有将订单链接到其客户的"rel":"客户"关系。它是否还考虑了实体之间的关系,例如一对多,多对多等。

它不同于一对多、多对多等实体之间的关系。

该关系描述当前资源与目标资源的关系。

以下是 https://restfulapi.net/hateoas/理解rel的一个很好的例子:

下面给出的 JSON 响应可能来自类似 APIHTTP GEThttp://api.domain.com/management/departments/10

{
"departmentId": 10,
"departmentName": "Administration",
"locationId": 1700,
"managerId": 200,
"links": [
{
"href": "10/employees",
"rel": "employees",
"type" : "GET"
}
]
}

在前面的示例中,服务器返回的响应包含指向员工资源 10/员工的超媒体链接,客户端可以遍历这些链接以读取属于该部门的员工。

它介于表示层和数据层之间。在rel和其他属性的帮助下创建的链接:

{
"href": "10/employees",
"rel": "employees",
"type" : "GET"
}

帮助应用程序转到正确的方向(存储库,方法等)以检索数据(属于该部门的员工)

根据您的设计,您还可以在数据层中的实体之间创建关系。但这些是不同的东西。

HATEOASAPI 中,rel"描述当前上下文(源)如何与目标资源相关">

https://restfulapi.net/hateoas/

最新更新