基礎上的樣本項目https://spring.io/guides/gs/accessing-data-rest/https://github.com/jcoig/gs-accessing-data-rest)我有庫定義如下:如何全局定製Spring Data REST資源的集合資源rel和路徑?
@RepositoryRestResource
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
這樣定義的庫通過http://localhost:8080/persons
可用並且反應是:
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons{?page,size,sort}",
"templated" : true
},
"search" : {
"href" : "http://localhost:8080/persons/search"
}
},
"_embedded" : {
"persons" : [ {
"firstName" : "John",
"lastName" : "Smith",
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons/1"
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
我不想在URL中有persons
,我不想讓persons
作爲返回的JSON中的關鍵字。當然,我也確定我的倉庫如下:
@RepositoryRestResource(collectionResourceRel = "key", path = "path")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
但我的問題是如何改變默認的Spring的行爲,得到了自定義鍵和自定義路徑提供商(就像例如禁用s
後綴)。
感謝您的回覆! 不幸我仍然得不到我想要的東西... 在 (沒有'collectionResourceRel'和'path')我添加了一個實現'RelProvider'的類,註解'Component'和'Order(value = Ordered.HIGHEST_PRECEDENCE)' 和三重寫的方法: 'getItemResourceRelFor'(返回'「iii」'), 'getCollectionResourceRelFor'(返回''ccc「')和 'supports'(返回'true')。 在'Application.java'中,我添加了bean,就像在你的答案中一樣......我仍然在JSON的鍵和URL中獲得了'persons'。 我在做什麼錯? –
jcoig
2014-09-29 05:59:25
這就是我們如何處理事情,它似乎工作。是否它所有的頁面都有項目,因此頁面界面被標準化。你能確認你的組件正在註冊爲bean嗎? (確保你的支持方法被調用)組件中的組件標註和bean方法是多餘的......但仍然可以工作。我們不需要執行訂單註釋。我們也是春季啓動 – 2014-10-07 19:52:13
克里斯,請看這個項目:https://github.com/jcoig/gs-accessing-data-rest。 它基於http://spring.io/guides/gs/accessing-data-rest/,基於奧利弗的提示進行更改(或者根據我的理解)。 – jcoig 2014-10-08 06:02:07