在Spring Data REST Query

时间:2016-02-18 16:50:35

标签: java rest jackson spring-data-mongodb spring-data-rest

是否可以控制在Spring Data REST中返回搜索结果时使用的类名?

我有一个类Account,它作为JSON模式发布,不包含id字段,因为它在RESTful API中应该是不透明的。为了使用Spring Data MongoDB持久保存,我将Account扩展为PersistableAccount,其中id字段。{/ p>

将搜索结果返回给客户端时,会公开名称persistableAccounts,这是一个不应泄漏到API中的实现细节:

{
  "_embedded" : {
    "persistableAccounts" : [ {
      "lastName" : "McLastName",
      "firstName" : "Kevin",
      "phoneNumber " : "+44 7700000000",
      "email" : "kevin@example.com",
      "_links" : {
        "self" : {
          "href" : "http://localhost:64712/accounts/id"
        },
        "persistableAccount" : {
          "href" : "http://localhost:64712/accounts/id"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:64712/accounts/search/findByFirstName?firstName=Kevin"
    }
  }
}

是否可以控制使用的术语?

1 个答案:

答案 0 :(得分:0)

如果您的结果可能包含多个子类型,则

Spring Data Rest在响应中始终包含 collectionRel

默认情况下,它是类名,但是,您可以在存储库的@RepositoryRestResource注释中对其进行自定义。

例如:

@RepositoryRestResource(collectionResourceRel = "whateverIwantHere")
public interface CustomerRepository extends CrudRepository<Customer, Long> {

}