问题描述
我正在尝试使用 Java Spring 构建 RESTful 服务架构,并为所有这些服务构建网关服务.为了实现后者,我需要为其他服务实现一个客户端,我和我的同事试图围绕 HATEOAS 原则设计,通过 spring-hateoas 模块提供相关资源的链接.
I'm trying to build an architecture of RESTful services, and to build a gateway service for all of those, with Java Spring. In order to make the latter, I need to implement a client for the other services, which me and my colleagues tried to design around the HATEOAS principle, by providing links to related resources through spring-hateoas module.
假设我有一个在本地主机上运行的服务,侦听 8080 端口,它通过 /resources
上的 GET 操作返回资源集合.例如:
Let's say I have a service running on localhost, listening on 8080 port, which returns a collection of resources with a GET operation on /resources
. For example:
{
"_embedded" : {
"resources" : [ {
"label" : "My first resource!",
"resourceId" : 3,
"_links" : {
"self" : {
"href" : "http://localhost:8080/resources/3"
},
"meals" : {
"href" : "http://localhost:8080/resources",
"templated" : true
}
}
}, {
"label" : "Another resource!",
"resourceId" : 4,
"_links" : {
"self" : {
"href" : "http://localhost:8080/resources/4"
},
"meals" : {
"href" : "http://localhost:8080/resources",
"templated" : true
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/resources",
"templated" : true
}
}
}
我正在尝试使用 HATEOAS 客户端,例如 Traverson.如何仅通过关注 HATEOAS 链接来关注资源元素?到目前为止,我的解决方案是在我的收藏中添加指向 item
的链接,例如:
I'm trying to use a HATEOAS client such as Traverson. How could I follow a resource element simply by following HATEOAS links? My solution so far has been to add a link to item
on my collection, such as follow:
"_links" : {
"self" : {
"href" : "http://localhost:8080/resources",
"templated" : true
},
"item" : {
"href" : "http://localhost:8080/resources/{id}",
"templated" : true
}
}
这样我就可以直接用 Traverson 替换模板中的 id 并跟踪结果.但这是一个好习惯吗?我应该换一种方式吗?
So then I can replace the id directly in the template with Traverson and follow the result. But is it a good practice? Should I proceed another way?
推荐答案
简单地说,Traverson 是为了找到一个链接.
Simply put, Traverson is meant to find a link.
在最简单的情况下,每个链接都有一个唯一的名称(rel
).通过简单地将 rel
的名称提供给 Traverson 的 follow(...)
函数,它将使用正确的 LinkDiscoverer
并导航到相应的该 rel
的 URI.
In the simplest cases, each link has a unique name (rel
). By simply providing the name of the rel
to Traverson's follow(...)
function, it will use the proper LinkDiscoverer
and navigate to the corresponding URI of that rel
.
这是一个Hop
.
由于目标是像跟踪网页上的链接一样导航 API,因此您必须定义一个跃点链.
Since the goal is to navigate the API kind of like following links on a webpage, you must define a chain of hops.
就您而言,情况稍微复杂一些,因为您嵌入了多个项目.请求 self
链接并不简单,因为您可以轻松地在根文档中看到三个.
In your case, it's a little more complication, since you have an embedded with multiple items. Asking for the self
link isn't straightforward, since you can easily see three on the root document.
因此 Traverson 支持 JSON-Path.如果您查看参考文档,很容易看出可以提供 JSON-Path 表达式来帮助选择您想要的链接.
Hence Traverson's support for JSON-Path. If you check the reference documentation, it's easy to see that a JSON-Path expression can be supplied to help pick which link you want.
只要选择的属性是 URI,Traverson 就会跳"到它.
As long as the attribute being selected is the URI, then Traverson will "hop" to it.
注意:当简单地使用 rels 时,您可以在 follow(...)
中提供多个 rels 作为字符串.当使用其他任何东西时,例如 JSON-Path 表达式或 rel(...)
,然后使用一个 follow(...)
每跳.值得庆幸的是,这并不难理解你将每一跳放在单独的行上(同样,请参阅参考文档以获取示例).
NOTE: When simply using rels, you can supply multiple rels as strings in follow(...)
. When using anything else, like a JSON-Path expression or rel(...)
, then use one follow(...)
per hop. Thankfully, this isn't hard to read of you put each hop on a separate line (again, see ref docs for examples).
这篇关于如何通过 HATEOAS 链接访问 REST 集合的一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!