首页 > 解决方案 > 如何在 Spring Data Rest 中隐藏 _embedded 实体字段

问题描述

我运行基于Spring Boot Gradle Plugin的 SDR 应用程序。我最近从2.1.9.RELEASE升级到2.2.0.RELEASE。在没有 100% 确定这就是原因的情况下,SDR 现在似乎_embedded为每个资源公开了一个附加字段。新字段包含相关实体的数据。

例如,这是一个使用2.1.9.RELEASE公开的资源:

{
  "uuid": "77315176-cb4f-4126-8e8b-9007457a7ce1",
  "name": "root",
  "_links": {
    "self": {
      "href": "localhost/users/1"
    },
    "user": {
      "href": "localhost/users/1"
    },
    "group": {
      "href": "localhost/users/1/group"
    }
  }
}

与2.2.0.RELEASE公开的相同资源:

{
  "uuid": "77315176-cb4f-4126-8e8b-9007457a7ce1",
  "name": "root",
  "_embedded": {
    "group": {
      "uuid": "be43382c-7b03-4d28-9597-7284986f700b",
      "name": "admin"
    }
  },
  "_links": {
    "self": {
      "href": "localhost/users/1"
    },
    "user": {
      "href": "localhost/users/1"
    },
    "group": {
      "href": "localhost/users/1/group"
    }
  }
}

如果没有证据,我假设有以下缺点:

  1. 它大大增加了响应大小。我可以理解在某些用例中需要这些额外的数据,但是,在这种情况下,我更喜欢根据需要通过制作投影来手动公开它。
  2. 为了收集额外暴露的数据,需要额外的数据库事务,这会对性能产生负面影响。
  3. 默认情况下会公开更多公共 API,需要维护。同样,我更喜欢使用Projections以尽可能减少响应。

这是我的问题:

  1. 是否可以恢复以前的 API 格式,例如自定义新功能或完全关闭它?
  2. 我想知道这个设计的目的是什么。鉴于上述假设的缺点,新功能有哪些优势?

标签: javaspringspring-bootspring-data-restspring-boot-gradle-plugin

解决方案


原来是Spring Data Rest 错误。版本2.2.1.RELEASE包含一个修复程序,它恢复了通常的行为。


推荐阅读