是否可以控制在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"
}
}
}
是否可以控制使用的术语?
答案 0 :(得分:0)
Spring Data Rest在响应中始终包含 collectionRel 。
默认情况下,它是类名,但是,您可以在存储库的@RepositoryRestResource
注释中对其进行自定义。
例如:
@RepositoryRestResource(collectionResourceRel = "whateverIwantHere")
public interface CustomerRepository extends CrudRepository<Customer, Long> {
}