基于示例项目 https://github.com/jcoig/gs-accessing-data-rest 中的https://spring.io/guides/gs/accessing-data-rest/ )我的存储库定义如下:
@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
}
}
我不想persons
在 URL中有,也不想persons
在返回的 JSON 中作为键。当然,我可以如下定义我的存储库:
@RepositoryRestResource(collectionResourceRel = "key", path = "path")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
但我的问题是如何更改默认 Spring 的行为并获得自定义键和自定义路径提供程序(就像禁用s
后缀的示例)。