【发布时间】:2019-04-13 20:23:30
【问题描述】:
我正在尝试使用 Java Spring 构建 RESTful 服务架构,并为所有这些构建网关服务。为了实现后者,我需要为其他服务实现一个客户端,我和我的同事试图围绕 HATEOAS 原则进行设计,通过 spring-hateoas 模块提供相关资源的链接。
假设我有一个在 localhost 上运行的服务,监听 8080 端口,它返回一个资源集合,并在 /resources 上执行 GET 操作。例如:
{
"_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 的链接,如下所示:
"_links" : {
"self" : {
"href" : "http://localhost:8080/resources",
"templated" : true
},
"item" : {
"href" : "http://localhost:8080/resources/{id}",
"templated" : true
}
}
那么我可以直接用 Traverson 替换模板中的 id 并按照结果进行操作。但这是一个好习惯吗?我应该采取其他方式吗?
【问题讨论】:
-
这个问题听起来很像this oneIMO
标签: spring rest hateoas spring-hateoas