【问题标题】:Spring Data REST will export all its attributes (except the id).Spring Data REST 将导出其所有属性(id 除外)。
【发布时间】:2018-06-11 12:57:55
【问题描述】:

我目前处于 Spring Data Rest 的学习阶段。 我开发了一个应用程序,在其中我能够成功地公开 Rest Respoitory 但没有“ID”。我在互联网上搜索以检查我是否做错了什么。但是,其中一个官方链接 (Official Doc) 说,“Spring Data REST 将导出其所有属性(id 除外)。您可以通过定义一个或多个投影来为 REST 服务的使用者提供替代方案。 " 因此,如果有十多个实体。我必须创建十个投影才能显示出来。有没有其他方法可以实现?

【问题讨论】:

  • 从客户端的角度来看,URI 就是 ID。不需要数据库 ID。

标签: java spring spring-data-rest spring-hateoas


【解决方案1】:

Spring Data REST 假定使用HATEOAS,因此每个资源都必须有一个带有其 ID 的自引用。这就是资源没有 ID 的原因。

但是你可以用RepositoryRestConfigurerAdapterconfigureRepositoryRestConfiguration方法开启它:

@Component
public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter {

  @Override
  public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
    config.exposeIdsFor(MyEntity1.class, MyEntity2.class);
  }
}

更多信息:12

【讨论】:

  • 查看this,以防您需要公开所有实体的标识符,或者仅针对那些扩展或实现特定超类或接口的实体。
猜你喜欢
  • 2015-09-03
  • 2014-01-29
  • 2017-07-04
  • 2016-01-06
  • 2020-10-15
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
相关资源
最近更新 更多