是否可以控制在 Spring Data REST 中返回搜索结果时使用的类名?
我有一个类 Account
,它作为 JSON 架构发布,并且不包含 id
字段,因为该字段在 RESTful API 中应该是不透明的。为了使用 Spring Data MongoDB 持久化此操作,我使用具有 id
字段的 PersistableAccount
扩展了 Account
。
将搜索结果返回给客户端时,名称 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"
}
}
}
是否可以控制所使用的术语?
最佳答案
如果您的结果可能包含多个子类型,Spring Data Rest 始终在响应中包含一个collectionRel。
默认情况下它是类名称,但是,您可以在存储库上的 @RepositoryRestResource
注释中自定义它。
例如:
@RepositoryRestResource(collectionResourceRel = "whateverIwantHere")
public interface CustomerRepository extends CrudRepository<Customer, Long> {
}
关于java - Spring Data REST 查询中返回类型的控制名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487561/