如何从 Spring Data REST 生成的表示中删除超媒体元素?

How to remove hypermedia elements from representations produced by Spring Data REST?

当我的 REST API 使用 Spring 数据时,当前返回的响应包括 _links 字段:

{
  "_embedded": {
  "users": [
    {
      "imageUrl": "some_image_url",
      "name": "some name",
      "id": "57420b2a0d31bb6cef4ee8e9",
      "_links": {
        "self": {
          "href": "http://localhost:8080/users/57420b2a0d31bb6cef4ee8e9"
        },
        "user": {
          "href": "http://localhost:8080/users/57420b2a0d31bb6cef4ee8e9{?projection}",
          "templated": true
        }
      }
    },
...

有没有一种方法可以产生隐藏 _links 字段的输出?例如:

{
  "_embedded": {
  "users": [
    {
      "imageUrl": "some_image_url",
      "name": "some name",
      "id": "57420b2a0d31bb6cef4ee8e9",
    },
...

我发现因为我公开了 id 字段,所以 _links 并不是真正必要的,而且大部分只是让我的回复变得混乱。

没有。超媒体是 REST API 的一个基本特征,Spring Data REST 大量使用它来允许您构建可以使用响应中存在的链接导航到相关资源的客户端。

当然你可以让你的客户变得愚蠢,不要使用这些信息,但这会导致更紧密的耦合(因为你不能再更改服务器端的 URI,你的客户希望与专用服务器,而使用超媒体,您可以将其指向不同的服务器等)。

与许多其他自称的 REST 框架相比,框架设计的一个关键方面是尊重 REST 中的基本原则并明确利用它们。或者至少,不要创造轻易打破它们的动机。 reference documentation and on the project website. Find out more about key design decisions in this presentation on Spring Data REST, and this one on Domain-Driven Design & REST.

中清楚地表达了这一点